blob: 9933ed1976e197ab5d2a526943782b45965af801 [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"
20#include "lldb/Symbol/SymbolVendor.h"
Sean Callanan673f3db2011-11-30 22:11:59 +000021#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000022#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023
24using namespace clang;
25using namespace lldb_private;
26
Greg Claytonb01000f2011-01-17 03:46:26 +000027ClangASTSource::~ClangASTSource()
28{
Sean Callanana3d04472011-11-29 00:42:02 +000029 m_ast_importer->ForgetDestination(m_ast_context);
30
Johnny Chenfa21ffd2011-11-30 23:18:53 +000031 // We are in the process of destruction, don't create clang ast context on demand
32 // by passing false to Target::GetScratchClangASTContext(create_on_demand).
33 ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext(false);
Sean Callanana3d04472011-11-29 00:42:02 +000034
35 if (!scratch_clang_ast_context)
36 return;
37
38 clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
39
40 if (!scratch_ast_context)
41 return;
42
43 if (m_ast_context != scratch_ast_context)
44 m_ast_importer->ForgetSource(scratch_ast_context, m_ast_context);
Greg Claytonb01000f2011-01-17 03:46:26 +000045}
Chris Lattner24943d22010-06-08 16:52:24 +000046
Greg Claytonb01000f2011-01-17 03:46:26 +000047void
48ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
49{
Sean Callananf76afff2011-10-28 23:38:38 +000050 if (!m_ast_context)
51 return;
52
53 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
54 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000055}
56
Chris Lattner24943d22010-06-08 16:52:24 +000057// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000058DeclContext::lookup_result
59ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000060(
61 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000062 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000063)
64{
Sean Callananf76afff2011-10-28 23:38:38 +000065 if (!m_ast_context)
66 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
67
68 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000069 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
70
71 std::string decl_name (clang_decl_name.getAsString());
72
73// if (m_decl_map.DoingASTImport ())
74// return DeclContext::lookup_result();
75//
Greg Clayton8de27c72010-10-15 22:48:33 +000076 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000077 // Normal identifiers.
78 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000079 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
80 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
81 break;
Chris Lattner24943d22010-06-08 16:52:24 +000082
83 // Operator names. Not important for now.
84 case DeclarationName::CXXOperatorName:
85 case DeclarationName::CXXLiteralOperatorName:
86 return DeclContext::lookup_result();
87
88 // Using directives found in this context.
89 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
90 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000091 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000092
Chris Lattner24943d22010-06-08 16:52:24 +000093 case DeclarationName::ObjCZeroArgSelector:
94 case DeclarationName::ObjCOneArgSelector:
95 case DeclarationName::ObjCMultiArgSelector:
Sean Callanan9b714842011-11-09 19:33:21 +000096 {
97 llvm::SmallVector<NamedDecl*, 1> method_decls;
Chris Lattner24943d22010-06-08 16:52:24 +000098
Sean Callanan9b714842011-11-09 19:33:21 +000099 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
100
101 FindObjCMethodDecls(method_search_context);
102
103 return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
104 }
Chris Lattner24943d22010-06-08 16:52:24 +0000105 // These aren't possible in the global context.
106 case DeclarationName::CXXConstructorName:
107 case DeclarationName::CXXDestructorName:
108 case DeclarationName::CXXConversionFunctionName:
109 return DeclContext::lookup_result();
110 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000111
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000112
Sean Callananf76afff2011-10-28 23:38:38 +0000113 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +0000114 {
115 // Wait until we see a '$' at the start of a name before we start doing
116 // any lookups so we can avoid lookup up all of the builtin types.
117 if (!decl_name.empty() && decl_name[0] == '$')
118 {
Sean Callananf76afff2011-10-28 23:38:38 +0000119 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +0000120 }
121 else
122 {
123 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
124 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000125 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000126
Greg Clayton8de27c72010-10-15 22:48:33 +0000127 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000128
129 const char *uniqued_const_decl_name = const_decl_name.GetCString();
130 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
131 {
132 // We are currently looking up this name...
133 return DeclContext::lookup_result();
134 }
135 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000136// static uint32_t g_depth = 0;
137// ++g_depth;
138// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000139 llvm::SmallVector<NamedDecl*, 4> name_decls;
140 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000141 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000142 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000143// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000144 m_active_lookups.erase (uniqued_const_decl_name);
145 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000146}
147
Greg Claytonb01000f2011-01-17 03:46:26 +0000148void
149ClangASTSource::CompleteType (TagDecl *tag_decl)
Sean Callananbb715f92011-10-29 02:28:18 +0000150{
151 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
152
Sean Callananb2027ec2011-12-08 23:45:45 +0000153 static unsigned int invocation_id = 0;
154 unsigned int current_id = invocation_id++;
155
Sean Callananbb715f92011-10-29 02:28:18 +0000156 if (log)
157 {
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000158 log->Printf(" CompleteTagDecl[%u] on (ASTContext*)%p Completing (TagDecl*)%p named %s",
159 current_id,
Sean Callananb2027ec2011-12-08 23:45:45 +0000160 m_ast_context,
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000161 tag_decl,
Sean Callananb2027ec2011-12-08 23:45:45 +0000162 tag_decl->getName().str().c_str());
163
164 log->Printf(" CTD[%u] Before:", current_id);
Sean Callananbb715f92011-10-29 02:28:18 +0000165 ASTDumper dumper((Decl*)tag_decl);
166 dumper.ToLog(log, " [CTD] ");
167 }
168
Sean Callananb2027ec2011-12-08 23:45:45 +0000169 if (!m_ast_importer->CompleteTagDecl (tag_decl))
170 {
171 // We couldn't complete the type. Maybe there's a definition
172 // somewhere else that can be completed.
173
174 if (log)
175 log->Printf(" CTD[%u] Type could not be completed in the module in which it was first found.", current_id);
176
177 bool found = false;
178
179 DeclContext *decl_ctx = tag_decl->getDeclContext();
180
181 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(decl_ctx))
182 {
183 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
184
185 if (log && log->GetVerbose())
186 log->Printf(" CTD[%u] Inspecting namespace map %p (%d entries)",
187 current_id,
188 namespace_map.get(),
189 (int)namespace_map->size());
190
191 if (!namespace_map)
192 return;
193
194 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
195 i != e && !found;
196 ++i)
197 {
198 if (log)
199 log->Printf(" CTD[%u] Searching namespace %s in module %s",
200 current_id,
201 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
202 i->first->GetFileSpec().GetFilename().GetCString());
203
204 TypeList types;
205
206 SymbolContext null_sc;
207 ConstString name(tag_decl->getName().str().c_str());
208
Greg Claytondc0a38c2012-03-26 23:03:23 +0000209 i->first->FindTypesInNamespace(null_sc, name, &i->second, UINT32_MAX, types);
Sean Callananb2027ec2011-12-08 23:45:45 +0000210
211 for (uint32_t ti = 0, te = types.GetSize();
212 ti != te && !found;
213 ++ti)
214 {
215 lldb::TypeSP type = types.GetTypeAtIndex(ti);
216
217 if (!type)
218 continue;
219
220 lldb::clang_type_t opaque_type = type->GetClangFullType();
221
222 if (!opaque_type)
223 continue;
224
Sean Callanan21f2e192011-12-14 01:13:04 +0000225 const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>();
Sean Callananb2027ec2011-12-08 23:45:45 +0000226
227 if (!tag_type)
228 continue;
229
230 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
231
232 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
233 found = true;
234 }
235 }
236 }
237 else
238 {
239 TypeList types;
240
241 SymbolContext null_sc;
242 ConstString name(tag_decl->getName().str().c_str());
243 ClangNamespaceDecl namespace_decl;
244
245 ModuleList &module_list = m_target->GetImages();
246
Greg Claytondc0a38c2012-03-26 23:03:23 +0000247 bool exact_match = false;
248 module_list.FindTypes2 (null_sc, name, exact_match, UINT32_MAX, types);
Sean Callananb2027ec2011-12-08 23:45:45 +0000249
250 for (uint32_t ti = 0, te = types.GetSize();
251 ti != te && !found;
252 ++ti)
253 {
254 lldb::TypeSP type = types.GetTypeAtIndex(ti);
255
256 if (!type)
257 continue;
258
259 lldb::clang_type_t opaque_type = type->GetClangFullType();
260
261 if (!opaque_type)
262 continue;
263
Sean Callanan21f2e192011-12-14 01:13:04 +0000264 const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>();
Sean Callananb2027ec2011-12-08 23:45:45 +0000265
266 if (!tag_type)
267 continue;
268
269 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
270
271 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
272 found = true;
273 }
274 }
275 }
Sean Callananbb715f92011-10-29 02:28:18 +0000276
277 if (log)
278 {
279 log->Printf(" [CTD] After:");
280 ASTDumper dumper((Decl*)tag_decl);
281 dumper.ToLog(log, " [CTD] ");
282 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000283}
284
285void
Sean Callananbb715f92011-10-29 02:28:18 +0000286ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
287{
288 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
289
290 if (log)
291 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000292 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 +0000293 log->Printf(" [COID] Before:");
294 ASTDumper dumper((Decl*)interface_decl);
295 dumper.ToLog(log, " [COID] ");
296 }
297
298 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
299
300 if (log)
301 {
302 log->Printf(" [COID] After:");
303 ASTDumper dumper((Decl*)interface_decl);
304 dumper.ToLog(log, " [COID] ");
305 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000306}
307
Sean Callanan9b6898f2011-07-30 02:42:06 +0000308clang::ExternalLoadResult
Sean Callananbb715f92011-10-29 02:28:18 +0000309ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
310 bool (*predicate)(Decl::Kind),
311 llvm::SmallVectorImpl<Decl*> &decls)
312{
313 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
314
315 const Decl *context_decl = dyn_cast<Decl>(decl_context);
316
317 if (!context_decl)
318 return ELR_Failure;
319
320 static unsigned int invocation_id = 0;
321 unsigned int current_id = invocation_id++;
322
323 if (log)
324 {
325 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000326 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000327 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000328 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000329 context_named_decl->getNameAsString().c_str(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000330 context_decl->getDeclKindName(),
331 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000332 (predicate ? "non-null" : "null"));
333 else if(context_decl)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000334 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%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_decl->getDeclKindName(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000338 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000339 (predicate ? "non-null" : "null"));
340 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000341 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000342 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000343 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000344 (predicate ? "non-null" : "null"));
345 }
346
347 Decl *original_decl = NULL;
348 ASTContext *original_ctx = NULL;
349
350 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
351 return ELR_Failure;
352
353 if (log)
354 {
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000355 log->Printf(" FELD[%u] Original decl (Decl*)%p:", current_id, original_decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000356 ASTDumper(original_decl).ToLog(log, " ");
357 }
358
359 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
360 {
361 ExternalASTSource *external_source = original_ctx->getExternalSource();
362
363 if (external_source)
364 external_source->CompleteType (original_tag_decl);
365 }
366
Sean Callananb2027ec2011-12-08 23:45:45 +0000367 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000368
369 if (!original_decl_context)
370 return ELR_Failure;
371
372 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
373 iter != original_decl_context->decls_end();
374 ++iter)
375 {
376 Decl *decl = *iter;
377
378 if (!predicate || predicate(decl->getKind()))
379 {
380 if (log)
381 {
382 ASTDumper ast_dumper(decl);
383 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
384 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
385 else
386 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
387 }
388
Sean Callanan4938bd62011-11-16 18:20:47 +0000389 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000390
Sean Callananddb59332012-03-20 21:11:12 +0000391 if (!copied_decl)
392 continue;
393
Sean Callanane9478392012-03-15 01:53:17 +0000394 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
395 {
396 QualType copied_field_type = copied_field->getType();
397
398 if (const TagType *copied_field_tag_type = copied_field_type->getAs<TagType>())
399 m_ast_importer->CompleteTagDecl(copied_field_tag_type->getDecl());
400 if (const ObjCObjectType *copied_field_object_type = copied_field_type->getAs<ObjCObjectType>())
401 if (ObjCInterfaceDecl *copied_field_objc_interface_decl = copied_field_object_type->getInterface())
402 m_ast_importer->CompleteObjCInterfaceDecl(copied_field_objc_interface_decl);
403 }
404
Sean Callananbb715f92011-10-29 02:28:18 +0000405 decls.push_back(copied_decl);
406 }
407 }
408
409 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000410}
411
Sean Callanan9394b5a2011-10-29 19:50:43 +0000412void
413ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
414{
415 assert (m_ast_context);
416
417 const ConstString name(context.m_decl_name.getAsString().c_str());
418
419 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
420
421 static unsigned int invocation_id = 0;
422 unsigned int current_id = invocation_id++;
423
424 if (log)
425 {
426 if (!context.m_decl_context)
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000427 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 +0000428 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000429 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 +0000430 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000431 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 +0000432 }
433
434 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
435
436 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
437 {
438 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
439
440 if (log && log->GetVerbose())
441 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
442 current_id,
443 namespace_map.get(),
444 (int)namespace_map->size());
445
446 if (!namespace_map)
447 return;
448
449 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
450 i != e;
451 ++i)
452 {
453 if (log)
454 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
455 current_id,
456 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
457 i->first->GetFileSpec().GetFilename().GetCString());
458
459 FindExternalVisibleDecls(context,
460 i->first,
461 i->second,
462 current_id);
463 }
464 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000465 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000466 {
Sean Callanan8f2e3922012-02-04 08:49:35 +0000467 FindObjCPropertyAndIvarDecls(context);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000468 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000469 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
470 {
471 // we shouldn't be getting FindExternalVisibleDecls calls for these
472 return;
473 }
474 else
475 {
476 ClangNamespaceDecl namespace_decl;
477
478 if (log)
479 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
480
481 FindExternalVisibleDecls(context,
482 lldb::ModuleSP(),
483 namespace_decl,
484 current_id);
485 }
486
487 if (!context.m_namespace_map->empty())
488 {
489 if (log && log->GetVerbose())
490 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
491 current_id,
492 context.m_namespace_map.get(),
493 (int)context.m_namespace_map->size());
494
495 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
496
497 if (clang_namespace_decl)
498 clang_namespace_decl->setHasExternalVisibleStorage();
499 }
500}
501
502void
503ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
504 lldb::ModuleSP module_sp,
505 ClangNamespaceDecl &namespace_decl,
506 unsigned int current_id)
507{
508 assert (m_ast_context);
509
510 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
511
512 SymbolContextList sc_list;
513
514 const ConstString name(context.m_decl_name.getAsString().c_str());
515
516 const char *name_unique_cstr = name.GetCString();
517
Sean Callanan8f2e3922012-02-04 08:49:35 +0000518 static ConstString id_name("id");
519 static ConstString Class_name("Class");
520
521 if (name == id_name || name == Class_name)
522 return;
523
Sean Callanan9394b5a2011-10-29 19:50:43 +0000524 if (name_unique_cstr == NULL)
525 return;
526
527 // The ClangASTSource is not responsible for finding $-names.
528 if (name_unique_cstr[0] == '$')
529 return;
530
531 if (module_sp && namespace_decl)
532 {
533 ClangNamespaceDecl found_namespace_decl;
534
535 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
536
537 if (symbol_vendor)
538 {
539 SymbolContext null_sc;
540
541 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
542
543 if (found_namespace_decl)
544 {
545 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
546
547 if (log)
548 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
549 current_id,
550 name.GetCString(),
551 module_sp->GetFileSpec().GetFilename().GetCString());
552 }
553 }
554 }
555 else
556 {
557 ModuleList &images = m_target->GetImages();
558
559 for (uint32_t i = 0, e = images.GetSize();
560 i != e;
561 ++i)
562 {
563 lldb::ModuleSP image = images.GetModuleAtIndex(i);
564
565 if (!image)
566 continue;
567
568 ClangNamespaceDecl found_namespace_decl;
569
570 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
571
572 if (!symbol_vendor)
573 continue;
574
575 SymbolContext null_sc;
576
577 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
578
579 if (found_namespace_decl)
580 {
581 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
582
583 if (log)
584 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
585 current_id,
586 name.GetCString(),
587 image->GetFileSpec().GetFilename().GetCString());
588 }
589 }
590 }
591
Sean Callanan9394b5a2011-10-29 19:50:43 +0000592 do
593 {
594 TypeList types;
595 SymbolContext null_sc;
Greg Claytondc0a38c2012-03-26 23:03:23 +0000596 const bool exact_match = false;
Sean Callanan0f71d192011-12-19 19:38:39 +0000597
Sean Callanan9394b5a2011-10-29 19:50:43 +0000598 if (module_sp && namespace_decl)
Greg Claytondc0a38c2012-03-26 23:03:23 +0000599 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
Sean Callanan0f71d192011-12-19 19:38:39 +0000600 else
Greg Claytondc0a38c2012-03-26 23:03:23 +0000601 m_target->GetImages().FindTypes2(null_sc, name, exact_match, 1, types);
Sean Callanan9394b5a2011-10-29 19:50:43 +0000602
603 if (types.GetSize())
604 {
605 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
606
607 if (log)
608 {
609 const char *name_string = type_sp->GetName().GetCString();
610
611 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
612 current_id,
613 name.GetCString(),
614 (name_string ? name_string : "<anonymous>"));
615 }
616
Sean Callanane1301a62011-12-06 03:41:14 +0000617
Sean Callanan9394b5a2011-10-29 19:50:43 +0000618 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
Sean Callanane1301a62011-12-06 03:41:14 +0000619
Sean Callanandc5fce12011-12-01 21:04:37 +0000620 if (!copied_type)
621 {
622 if (log)
Sean Callanane1301a62011-12-06 03:41:14 +0000623 log->Printf(" CAS::FEVD[%u] - Couldn't export the type for a constant integer result",
624 current_id);
625
Sean Callanandc5fce12011-12-01 21:04:37 +0000626 break;
627 }
628
Sean Callanan9394b5a2011-10-29 19:50:43 +0000629 context.AddTypeDecl(copied_type);
630 }
Sean Callanan673f3db2011-11-30 22:11:59 +0000631
Sean Callanan9394b5a2011-10-29 19:50:43 +0000632 } while(0);
633}
634
Sean Callanan9b714842011-11-09 19:33:21 +0000635void
636ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
637{
638 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
639
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000640 static unsigned int invocation_id = 0;
641 unsigned int current_id = invocation_id++;
642
Sean Callanan9b714842011-11-09 19:33:21 +0000643 const DeclarationName &decl_name(context.m_decl_name);
644 const DeclContext *decl_ctx(context.m_decl_context);
645
646 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
647
648 if (!interface_decl)
649 return;
650
651 StreamString ss;
Sean Callanane1301a62011-12-06 03:41:14 +0000652
Sean Callanan9b714842011-11-09 19:33:21 +0000653 if (decl_name.isObjCZeroArgSelector())
654 {
655 ss.Printf("%s", decl_name.getAsString().c_str());
656 }
657 else if (decl_name.isObjCOneArgSelector())
658 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000659 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000660 }
661 else
662 {
663 clang::Selector sel = decl_name.getObjCSelector();
664
665 for (unsigned i = 0, e = sel.getNumArgs();
666 i != e;
667 ++i)
668 {
669 llvm::StringRef r = sel.getNameForSlot(i);
670 ss.Printf("%s:", r.str().c_str());
671 }
672 }
673 ss.Flush();
674
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000675 ConstString selector_name(ss.GetData());
676
Sean Callanan9b714842011-11-09 19:33:21 +0000677 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000678 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
679 current_id,
680 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000681 interface_decl->getNameAsString().c_str(),
682 selector_name.AsCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000683 SymbolContextList sc_list;
684
685 const bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000686 const bool include_inlines = false;
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000687 const bool append = false;
688
Sean Callanane0028b82012-01-19 02:17:40 +0000689 std::string interface_name = interface_decl->getNameAsString();
690
691 do
692 {
693 StreamString ms;
694 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
695 ms.Flush();
696 ConstString instance_method_name(ms.GetData());
697
Sean Callanan302d78c2012-02-10 22:52:19 +0000698 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000699
700 if (sc_list.GetSize())
701 break;
702
703 ms.Clear();
704 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
705 ms.Flush();
706 ConstString class_method_name(ms.GetData());
707
Sean Callanan302d78c2012-02-10 22:52:19 +0000708 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000709
710 if (sc_list.GetSize())
711 break;
712
713 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
714 // categories on the desired class.
715
716 SymbolContextList candidate_sc_list;
717
Sean Callanan302d78c2012-02-10 22:52:19 +0000718 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000719
720 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
721 ci != ce;
722 ++ci)
723 {
724 SymbolContext candidate_sc;
725
726 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
727 continue;
728
729 if (!candidate_sc.function)
730 continue;
731
732 const char *candidate_name = candidate_sc.function->GetName().AsCString();
733
734 const char *cursor = candidate_name;
735
736 if (*cursor != '+' && *cursor != '-')
737 continue;
738
739 ++cursor;
740
741 if (*cursor != '[')
742 continue;
743
744 ++cursor;
745
746 size_t interface_len = interface_name.length();
747
748 if (strncmp(cursor, interface_name.c_str(), interface_len))
749 continue;
750
751 cursor += interface_len;
752
753 if (*cursor == ' ' || *cursor == '(')
754 sc_list.Append(candidate_sc);
755 }
756 }
757 while (0);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000758
759 for (uint32_t i = 0, e = sc_list.GetSize();
760 i != e;
761 ++i)
762 {
763 SymbolContext sc;
764
765 if (!sc_list.GetContextAtIndex(i, sc))
766 continue;
767
768 if (!sc.function)
769 continue;
770
771 DeclContext *function_ctx = sc.function->GetClangDeclContext();
772
773 if (!function_ctx)
774 continue;
775
776 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
777
778 if (!method_decl)
779 continue;
780
781 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
782
783 if (!found_interface_decl)
784 continue;
785
786 if (found_interface_decl->getName() == interface_decl->getName())
787 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000788 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000789
790 if (!copied_decl)
791 continue;
792
793 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
794
795 if (!copied_method_decl)
796 continue;
797
798 if (log)
799 {
800 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanane1301a62011-12-06 03:41:14 +0000801 log->Printf(" CAS::FOMD[%d] found (in debug info) %s", current_id, dumper.GetCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000802 }
Sean Callanane0028b82012-01-19 02:17:40 +0000803
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000804 context.AddNamedDecl(copied_method_decl);
805 }
806 }
Sean Callanan9b714842011-11-09 19:33:21 +0000807}
808
Sean Callanan8f2e3922012-02-04 08:49:35 +0000809template <class D> class TaggedASTDecl {
810public:
811 TaggedASTDecl() : decl(NULL) { }
812 TaggedASTDecl(D *_decl) : decl(_decl) { }
813 bool IsValid() const { return (decl != NULL); }
814 bool IsInvalid() const { return !IsValid(); }
815 D *operator->() const { return decl; }
816 D *decl;
817};
818
819template <class D2, template <class D> class TD, class D1>
820TD<D2>
821DynCast(TD<D1> source)
822{
823 return TD<D2> (dyn_cast<D2>(source.decl));
824}
825
826template <class D = Decl> class DeclFromParser;
827template <class D = Decl> class DeclFromUser;
828
829template <class D> class DeclFromParser : public TaggedASTDecl<D> {
830public:
831 DeclFromParser() : TaggedASTDecl<D>() { }
832 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
833
834 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
835};
836
837template <class D> class DeclFromUser : public TaggedASTDecl<D> {
838public:
839 DeclFromUser() : TaggedASTDecl<D>() { }
840 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
841
842 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
843};
844
845template <class D>
846DeclFromUser<D>
847DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
848{
849 DeclFromUser <> origin_decl;
850 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
851 if (origin_decl.IsInvalid())
852 return DeclFromUser<D>();
853 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
854}
855
856template <class D>
857DeclFromParser<D>
858DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
859{
860 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
861 if (parser_generic_decl.IsInvalid())
862 return DeclFromParser<D>();
863 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
864}
865
Sean Callanan931acec2012-02-22 23:57:45 +0000866static bool
867FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
868 NameSearchContext &context,
869 clang::ASTContext &ast_context,
870 ClangASTImporter *ast_importer,
871 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
872{
873 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
874
875 if (origin_iface_decl.IsInvalid())
876 return false;
877
878 std::string name_str = context.m_decl_name.getAsString();
879 StringRef name(name_str.c_str());
880 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
881
882 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
883
884 bool found = false;
885
886 if (origin_property_decl.IsValid())
887 {
888 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
889 if (parser_property_decl.IsValid())
890 {
891 if (log)
892 {
893 ASTDumper dumper((Decl*)parser_property_decl.decl);
894 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
895 }
896
897 context.AddNamedDecl(parser_property_decl.decl);
898 found = true;
899 }
900 }
901
902 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
903
904 if (origin_ivar_decl.IsValid())
905 {
906 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
907 if (parser_ivar_decl.IsValid())
908 {
909 if (log)
910 {
911 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
912 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
913 }
914
915 context.AddNamedDecl(parser_ivar_decl.decl);
916 found = true;
917 }
918 }
919
920 return found;
921}
922
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000923void
Sean Callanan8f2e3922012-02-04 08:49:35 +0000924ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000925{
926 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
927
928 static unsigned int invocation_id = 0;
929 unsigned int current_id = invocation_id++;
930
Sean Callanan8f2e3922012-02-04 08:49:35 +0000931 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
932 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan931acec2012-02-22 23:57:45 +0000933
934 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000935
936 if (log)
Sean Callanan8f2e3922012-02-04 08:49:35 +0000937 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000938 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000939 m_ast_context,
Sean Callanan8f2e3922012-02-04 08:49:35 +0000940 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan931acec2012-02-22 23:57:45 +0000941 context.m_decl_name.getAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000942
Sean Callanan931acec2012-02-22 23:57:45 +0000943 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
944 context,
945 *m_ast_context,
946 m_ast_importer,
947 origin_iface_decl))
948 return;
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000949
Sean Callanan931acec2012-02-22 23:57:45 +0000950 if (log)
951 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
952 current_id,
953 origin_iface_decl.decl,
954 &origin_iface_decl->getASTContext());
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000955
Sean Callanan931acec2012-02-22 23:57:45 +0000956 SymbolContext null_sc;
957 TypeList type_list;
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000958
Sean Callanan931acec2012-02-22 23:57:45 +0000959 lldb::ProcessSP process(m_target->GetProcessSP());
960
961 if (!process)
962 return;
963
964 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
965
966 if (!language_runtime)
967 return;
968
969 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
970
971 if (!complete_type_sp)
972 return;
973
974 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType(), complete_type_sp->GetClangAST());
975 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
976
977 if (!complete_opaque_type)
978 return;
979
980 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
981 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
982
983 if (!complete_interface_type)
984 return;
985
986 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_type->getDecl());
987
988 if (complete_iface_decl.decl == origin_iface_decl.decl)
989 return; // already checked this one
990
991 if (log)
992 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
993 current_id,
994 complete_iface_decl.decl,
995 &complete_iface_decl->getASTContext());
996
997
998 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
999 context,
1000 *m_ast_context,
1001 m_ast_importer,
1002 complete_iface_decl))
1003 return;
Sean Callanan8f2e3922012-02-04 08:49:35 +00001004}
1005
1006typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1007typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1008
1009template <class D, class O>
1010static bool
1011ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1012 llvm::DenseMap <const D*, O> &source_map,
1013 ClangASTImporter *importer,
1014 ASTContext &dest_ctx)
1015{
1016 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001017
Sean Callanan8f2e3922012-02-04 08:49:35 +00001018 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1019 fi != fe;
1020 ++fi)
1021 {
1022 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1023 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1024 if (parser_decl.IsInvalid())
1025 return false;
1026 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1027 }
1028
1029 return true;
1030}
1031
1032template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1033 DeclFromUser<const CXXRecordDecl> &record,
1034 BaseOffsetMap &base_offsets)
1035{
1036 for (CXXRecordDecl::base_class_const_iterator
1037 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1038 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1039 bi != be;
1040 ++bi)
1041 {
1042 if (!IsVirtual && bi->isVirtual())
1043 continue;
1044
1045 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1046 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1047
1048 if (!origin_base_record_type)
1049 return false;
1050
1051 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1052
1053 if (origin_base_record.IsInvalid())
1054 return false;
1055
1056 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1057
1058 if (origin_base_cxx_record.IsInvalid())
1059 return false;
1060
1061 CharUnits base_offset;
1062
1063 if (IsVirtual)
1064 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1065 else
1066 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1067
1068 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1069 }
1070
1071 return true;
1072}
1073
1074bool
1075ClangASTSource::layoutRecordType(const RecordDecl *record,
1076 uint64_t &size,
1077 uint64_t &alignment,
1078 FieldOffsetMap &field_offsets,
1079 BaseOffsetMap &base_offsets,
1080 BaseOffsetMap &virtual_base_offsets)
1081{
1082 static unsigned int invocation_id = 0;
1083 unsigned int current_id = invocation_id++;
1084
1085 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1086
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001087 if (log)
1088 {
Sean Callanan8f2e3922012-02-04 08:49:35 +00001089 log->Printf("LayoutRecordType[%u] on (RecordDecl*)%p [name = '%s']",
1090 current_id,
1091 m_ast_context,
1092 record->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001093 }
Sean Callanan8f2e3922012-02-04 08:49:35 +00001094
1095
1096 DeclFromParser <const RecordDecl> parser_record(record);
1097 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1098
1099 if (origin_record.IsInvalid())
1100 return false;
1101
1102 FieldOffsetMap origin_field_offsets;
1103 BaseOffsetMap origin_base_offsets;
1104 BaseOffsetMap origin_virtual_base_offsets;
1105
1106 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1107
1108 int field_idx = 0;
1109
1110 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1111 fi != fe;
1112 ++fi)
1113 {
1114 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1115
1116 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1117
1118 field_idx++;
1119 }
1120
1121 ASTContext &parser_ast_context(record->getASTContext());
1122
1123 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1124
1125 if (origin_cxx_record.IsValid())
1126 {
1127 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1128 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1129 return false;
1130 }
1131
1132 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1133 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1134 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1135 return false;
1136
1137 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1138 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1139
1140 if (log)
1141 {
1142 log->Printf("LRT[%u] returned:", current_id);
1143 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
1144 log->Printf("LRT[%u] Size = %lld", current_id, size);
1145 log->Printf("LRT[%u] Alignment = %lld", current_id, alignment);
1146 log->Printf("LRT[%u] Fields:", current_id);
1147 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1148 fi != fe;
1149 ++fi)
1150 {
1151 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %lld bits",
1152 current_id,
1153 *fi,
1154 fi->getNameAsString().c_str(),
1155 field_offsets[*fi]);
1156 }
1157 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1158 if (parser_cxx_record.IsValid())
1159 {
1160 log->Printf("LRT[%u] Bases:", current_id);
1161 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1162 bi != be;
1163 ++bi)
1164 {
1165 bool is_virtual = bi->isVirtual();
1166
1167 QualType base_type = bi->getType();
1168 const RecordType *base_record_type = base_type->getAs<RecordType>();
1169 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1170 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1171
1172 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %lld chars",
1173 current_id,
1174 (is_virtual ? "Virtual " : ""),
1175 base_cxx_record.decl,
1176 base_cxx_record.decl->getNameAsString().c_str(),
1177 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1178 base_offsets[base_cxx_record.decl].getQuantity()));
1179 }
1180 }
1181 else
1182 {
1183 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1184 }
1185 }
1186
1187 return true;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001188}
1189
Sean Callanan73b520f2011-10-29 01:58:46 +00001190void
1191ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1192 const ConstString &name,
1193 ClangASTImporter::NamespaceMapSP &parent_map) const
1194{
1195 static unsigned int invocation_id = 0;
1196 unsigned int current_id = invocation_id++;
1197
1198 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1199
1200 if (log)
1201 {
1202 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001203 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001204 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001205 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001206 name.GetCString(),
1207 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1208 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001209 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001210 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001211 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001212 name.GetCString());
1213 }
1214
1215
1216 if (parent_map)
1217 {
1218 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1219 i != e;
1220 ++i)
1221 {
1222 ClangNamespaceDecl found_namespace_decl;
1223
1224 lldb::ModuleSP module_sp = i->first;
1225 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1226
1227 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1228
1229 if (!symbol_vendor)
1230 continue;
1231
1232 SymbolContext null_sc;
1233
1234 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1235
1236 if (!found_namespace_decl)
1237 continue;
1238
1239 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1240
1241 if (log)
1242 log->Printf(" CMN[%u] Found namespace %s in module %s",
1243 current_id,
1244 name.GetCString(),
1245 module_sp->GetFileSpec().GetFilename().GetCString());
1246 }
1247 }
1248 else
1249 {
1250 ModuleList &images = m_target->GetImages();
1251 ClangNamespaceDecl null_namespace_decl;
1252
1253 for (uint32_t i = 0, e = images.GetSize();
1254 i != e;
1255 ++i)
1256 {
1257 lldb::ModuleSP image = images.GetModuleAtIndex(i);
1258
1259 if (!image)
1260 continue;
1261
1262 ClangNamespaceDecl found_namespace_decl;
1263
1264 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1265
1266 if (!symbol_vendor)
1267 continue;
1268
1269 SymbolContext null_sc;
1270
1271 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1272
1273 if (!found_namespace_decl)
1274 continue;
1275
1276 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1277
1278 if (log)
1279 log->Printf(" CMN[%u] Found namespace %s in module %s",
1280 current_id,
1281 name.GetCString(),
1282 image->GetFileSpec().GetFilename().GetCString());
1283 }
1284 }
1285}
1286
Sean Callananbb715f92011-10-29 02:28:18 +00001287NamespaceDecl *
1288ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1289{
Greg Clayton13d24fb2012-01-29 20:56:30 +00001290 if (!namespace_decls)
Sean Callananbb715f92011-10-29 02:28:18 +00001291 return NULL;
1292
1293 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1294
1295 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1296
Sean Callanan4938bd62011-11-16 18:20:47 +00001297 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +00001298
Sean Callananddb59332012-03-20 21:11:12 +00001299 if (!copied_decl)
1300 return NULL;
1301
Sean Callananbb715f92011-10-29 02:28:18 +00001302 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1303
1304 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1305
1306 return dyn_cast<NamespaceDecl>(copied_decl);
1307}
1308
Sean Callanan9394b5a2011-10-29 19:50:43 +00001309void *
1310ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1311 ASTContext *source_context,
1312 void *clang_type)
1313{
1314 SetImportInProgress(true);
1315
Sean Callanan4938bd62011-11-16 18:20:47 +00001316 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +00001317
1318 void *ret = ret_qual_type.getAsOpaquePtr();
1319
1320 SetImportInProgress(false);
1321
Sean Callananddb59332012-03-20 21:11:12 +00001322 if (ret && ret_qual_type->getCanonicalTypeInternal().isNull())
Sean Callananaa11af82012-02-27 19:22:39 +00001323 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1324 // on occasion.
1325 return NULL;
1326
Sean Callanan9394b5a2011-10-29 19:50:43 +00001327 return ret;
1328}
1329
Greg Claytonb01000f2011-01-17 03:46:26 +00001330clang::NamedDecl *
1331NameSearchContext::AddVarDecl(void *type)
1332{
Greg Clayton8de27c72010-10-15 22:48:33 +00001333 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +00001334
1335 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +00001336
Sean Callananf76afff2011-10-28 23:38:38 +00001337 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001338 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +00001339 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001340 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +00001341 ii,
Chris Lattner24943d22010-06-08 16:52:24 +00001342 QualType::getFromOpaquePtr(type),
1343 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001344 SC_Static,
1345 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +00001346 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +00001347
1348 return Decl;
1349}
Sean Callanan8f0dc342010-06-22 23:46:24 +00001350
Greg Claytonb01000f2011-01-17 03:46:26 +00001351clang::NamedDecl *
1352NameSearchContext::AddFunDecl (void *type)
1353{
Sean Callanan30a5dd52012-03-21 17:13:20 +00001354 assert (type && "Type for variable must be non-NULL!");
1355
Sean Callananf76afff2011-10-28 23:38:38 +00001356 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001357 const_cast<DeclContext*>(m_decl_context),
1358 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001359 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001360 m_decl_name.getAsIdentifierInfo(),
1361 QualType::getFromOpaquePtr(type),
1362 NULL,
1363 SC_Static,
1364 SC_Static,
1365 false,
1366 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001367
Sean Callananb291abe2010-08-12 23:45:38 +00001368 // We have to do more than just synthesize the FunctionDecl. We have to
1369 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1370 // this, we raid the function's FunctionProtoType for types.
1371
Greg Clayton8de27c72010-10-15 22:48:33 +00001372 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callanan21f2e192011-12-14 01:13:04 +00001373 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001374
Greg Clayton8de27c72010-10-15 22:48:33 +00001375 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +00001376 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001377 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001378 unsigned ArgIndex;
1379
Sean Callananc1535182011-10-07 23:18:13 +00001380 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1381
Sean Callanan8f0dc342010-06-22 23:46:24 +00001382 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1383 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001384 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001385
Sean Callananf76afff2011-10-28 23:38:38 +00001386 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +00001387 const_cast<DeclContext*>(m_decl_context),
1388 SourceLocation(),
1389 SourceLocation(),
1390 NULL,
1391 arg_qual_type,
1392 NULL,
1393 SC_Static,
1394 SC_Static,
1395 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001396 }
1397
Sean Callananc1535182011-10-07 23:18:13 +00001398 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001399 }
Sean Callanandc5fce12011-12-01 21:04:37 +00001400 else
1401 {
1402 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1403
1404 log->Printf("Function type wasn't a FunctionProtoType");
1405 }
Sean Callanan8f0dc342010-06-22 23:46:24 +00001406
Greg Clayton8de27c72010-10-15 22:48:33 +00001407 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001408
Greg Clayton8de27c72010-10-15 22:48:33 +00001409 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +00001410}
Sean Callanan0fc73582010-07-27 00:55:47 +00001411
Greg Claytonb01000f2011-01-17 03:46:26 +00001412clang::NamedDecl *
1413NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +00001414{
Sean Callananad293092011-01-18 23:32:05 +00001415 FunctionProtoType::ExtProtoInfo proto_info;
1416
1417 proto_info.Variadic = true;
1418
Sean Callananf76afff2011-10-28 23:38:38 +00001419 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1420 NULL, // argument types
1421 0, // number of arguments
1422 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +00001423
Sean Callanan0fc73582010-07-27 00:55:47 +00001424 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1425}
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001426
Greg Claytonb01000f2011-01-17 03:46:26 +00001427clang::NamedDecl *
1428NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001429{
Greg Claytona1aaaff2011-01-23 00:34:52 +00001430 if (type)
1431 {
1432 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001433
Sean Callanan21f2e192011-12-14 01:13:04 +00001434 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001435 {
1436 TagDecl *tag_decl = tag_type->getDecl();
1437
1438 m_decls.push_back(tag_decl);
1439
1440 return tag_decl;
1441 }
Sean Callanan21f2e192011-12-14 01:13:04 +00001442 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001443 {
1444 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1445
1446 m_decls.push_back((NamedDecl*)interface_decl);
1447
1448 return (NamedDecl*)interface_decl;
1449 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001450 }
Greg Claytona1aaaff2011-01-23 00:34:52 +00001451 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001452}
Greg Claytone6d72ca2011-06-25 00:44:06 +00001453
1454void
1455NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1456{
1457 for (clang::NamedDecl * const *decl_iterator = result.first;
1458 decl_iterator != result.second;
1459 ++decl_iterator)
1460 m_decls.push_back (*decl_iterator);
1461}
1462
1463void
1464NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1465{
1466 m_decls.push_back (decl);
1467}