blob: bfe876d0fa1990f5e11f5a9f51f5d266d45731d4 [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
209 i->first->FindTypes(null_sc, name, &i->second, true, UINT32_MAX, types);
210
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
247 module_list.FindTypes(null_sc, name, true, UINT32_MAX, types);
248
249 for (uint32_t ti = 0, te = types.GetSize();
250 ti != te && !found;
251 ++ti)
252 {
253 lldb::TypeSP type = types.GetTypeAtIndex(ti);
254
255 if (!type)
256 continue;
257
258 lldb::clang_type_t opaque_type = type->GetClangFullType();
259
260 if (!opaque_type)
261 continue;
262
Sean Callanan21f2e192011-12-14 01:13:04 +0000263 const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>();
Sean Callananb2027ec2011-12-08 23:45:45 +0000264
265 if (!tag_type)
266 continue;
267
268 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
269
270 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
271 found = true;
272 }
273 }
274 }
Sean Callananbb715f92011-10-29 02:28:18 +0000275
276 if (log)
277 {
278 log->Printf(" [CTD] After:");
279 ASTDumper dumper((Decl*)tag_decl);
280 dumper.ToLog(log, " [CTD] ");
281 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000282}
283
284void
Sean Callananbb715f92011-10-29 02:28:18 +0000285ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
286{
287 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
288
289 if (log)
290 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000291 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 +0000292 log->Printf(" [COID] Before:");
293 ASTDumper dumper((Decl*)interface_decl);
294 dumper.ToLog(log, " [COID] ");
295 }
296
297 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
298
299 if (log)
300 {
301 log->Printf(" [COID] After:");
302 ASTDumper dumper((Decl*)interface_decl);
303 dumper.ToLog(log, " [COID] ");
304 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000305}
306
Sean Callanan9b6898f2011-07-30 02:42:06 +0000307clang::ExternalLoadResult
Sean Callananbb715f92011-10-29 02:28:18 +0000308ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
309 bool (*predicate)(Decl::Kind),
310 llvm::SmallVectorImpl<Decl*> &decls)
311{
312 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
313
314 const Decl *context_decl = dyn_cast<Decl>(decl_context);
315
316 if (!context_decl)
317 return ELR_Failure;
318
319 static unsigned int invocation_id = 0;
320 unsigned int current_id = invocation_id++;
321
322 if (log)
323 {
324 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000325 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000326 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000327 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000328 context_named_decl->getNameAsString().c_str(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000329 context_decl->getDeclKindName(),
330 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000331 (predicate ? "non-null" : "null"));
332 else if(context_decl)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000333 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000334 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000335 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000336 context_decl->getDeclKindName(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000337 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000338 (predicate ? "non-null" : "null"));
339 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000340 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000341 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000342 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000343 (predicate ? "non-null" : "null"));
344 }
345
346 Decl *original_decl = NULL;
347 ASTContext *original_ctx = NULL;
348
349 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
350 return ELR_Failure;
351
352 if (log)
353 {
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000354 log->Printf(" FELD[%u] Original decl (Decl*)%p:", current_id, original_decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000355 ASTDumper(original_decl).ToLog(log, " ");
356 }
357
358 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
359 {
360 ExternalASTSource *external_source = original_ctx->getExternalSource();
361
362 if (external_source)
363 external_source->CompleteType (original_tag_decl);
364 }
365
Sean Callananb2027ec2011-12-08 23:45:45 +0000366 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000367
368 if (!original_decl_context)
369 return ELR_Failure;
370
371 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
372 iter != original_decl_context->decls_end();
373 ++iter)
374 {
375 Decl *decl = *iter;
376
377 if (!predicate || predicate(decl->getKind()))
378 {
379 if (log)
380 {
381 ASTDumper ast_dumper(decl);
382 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
383 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
384 else
385 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
386 }
387
Sean Callanan4938bd62011-11-16 18:20:47 +0000388 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000389
Sean Callanane9478392012-03-15 01:53:17 +0000390 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
391 {
392 QualType copied_field_type = copied_field->getType();
393
394 if (const TagType *copied_field_tag_type = copied_field_type->getAs<TagType>())
395 m_ast_importer->CompleteTagDecl(copied_field_tag_type->getDecl());
396 if (const ObjCObjectType *copied_field_object_type = copied_field_type->getAs<ObjCObjectType>())
397 if (ObjCInterfaceDecl *copied_field_objc_interface_decl = copied_field_object_type->getInterface())
398 m_ast_importer->CompleteObjCInterfaceDecl(copied_field_objc_interface_decl);
399 }
400
Sean Callananbb715f92011-10-29 02:28:18 +0000401 decls.push_back(copied_decl);
402 }
403 }
404
405 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000406}
407
Sean Callanan9394b5a2011-10-29 19:50:43 +0000408void
409ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
410{
411 assert (m_ast_context);
412
413 const ConstString name(context.m_decl_name.getAsString().c_str());
414
415 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
416
417 static unsigned int invocation_id = 0;
418 unsigned int current_id = invocation_id++;
419
420 if (log)
421 {
422 if (!context.m_decl_context)
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000423 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 +0000424 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000425 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 +0000426 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000427 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 +0000428 }
429
430 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
431
432 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
433 {
434 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
435
436 if (log && log->GetVerbose())
437 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
438 current_id,
439 namespace_map.get(),
440 (int)namespace_map->size());
441
442 if (!namespace_map)
443 return;
444
445 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
446 i != e;
447 ++i)
448 {
449 if (log)
450 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
451 current_id,
452 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
453 i->first->GetFileSpec().GetFilename().GetCString());
454
455 FindExternalVisibleDecls(context,
456 i->first,
457 i->second,
458 current_id);
459 }
460 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000461 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000462 {
Sean Callanan8f2e3922012-02-04 08:49:35 +0000463 FindObjCPropertyAndIvarDecls(context);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000464 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000465 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
466 {
467 // we shouldn't be getting FindExternalVisibleDecls calls for these
468 return;
469 }
470 else
471 {
472 ClangNamespaceDecl namespace_decl;
473
474 if (log)
475 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
476
477 FindExternalVisibleDecls(context,
478 lldb::ModuleSP(),
479 namespace_decl,
480 current_id);
481 }
482
483 if (!context.m_namespace_map->empty())
484 {
485 if (log && log->GetVerbose())
486 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
487 current_id,
488 context.m_namespace_map.get(),
489 (int)context.m_namespace_map->size());
490
491 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
492
493 if (clang_namespace_decl)
494 clang_namespace_decl->setHasExternalVisibleStorage();
495 }
496}
497
498void
499ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
500 lldb::ModuleSP module_sp,
501 ClangNamespaceDecl &namespace_decl,
502 unsigned int current_id)
503{
504 assert (m_ast_context);
505
506 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
507
508 SymbolContextList sc_list;
509
510 const ConstString name(context.m_decl_name.getAsString().c_str());
511
512 const char *name_unique_cstr = name.GetCString();
513
Sean Callanan8f2e3922012-02-04 08:49:35 +0000514 static ConstString id_name("id");
515 static ConstString Class_name("Class");
516
517 if (name == id_name || name == Class_name)
518 return;
519
Sean Callanan9394b5a2011-10-29 19:50:43 +0000520 if (name_unique_cstr == NULL)
521 return;
522
523 // The ClangASTSource is not responsible for finding $-names.
524 if (name_unique_cstr[0] == '$')
525 return;
526
527 if (module_sp && namespace_decl)
528 {
529 ClangNamespaceDecl found_namespace_decl;
530
531 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
532
533 if (symbol_vendor)
534 {
535 SymbolContext null_sc;
536
537 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
538
539 if (found_namespace_decl)
540 {
541 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
542
543 if (log)
544 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
545 current_id,
546 name.GetCString(),
547 module_sp->GetFileSpec().GetFilename().GetCString());
548 }
549 }
550 }
551 else
552 {
553 ModuleList &images = m_target->GetImages();
554
555 for (uint32_t i = 0, e = images.GetSize();
556 i != e;
557 ++i)
558 {
559 lldb::ModuleSP image = images.GetModuleAtIndex(i);
560
561 if (!image)
562 continue;
563
564 ClangNamespaceDecl found_namespace_decl;
565
566 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
567
568 if (!symbol_vendor)
569 continue;
570
571 SymbolContext null_sc;
572
573 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
574
575 if (found_namespace_decl)
576 {
577 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
578
579 if (log)
580 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
581 current_id,
582 name.GetCString(),
583 image->GetFileSpec().GetFilename().GetCString());
584 }
585 }
586 }
587
Sean Callanan9394b5a2011-10-29 19:50:43 +0000588 do
589 {
590 TypeList types;
591 SymbolContext null_sc;
Sean Callanan0f71d192011-12-19 19:38:39 +0000592
Sean Callanan9394b5a2011-10-29 19:50:43 +0000593 if (module_sp && namespace_decl)
594 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
Sean Callanan0f71d192011-12-19 19:38:39 +0000595 else
Sean Callanane1301a62011-12-06 03:41:14 +0000596 m_target->GetImages().FindTypes(null_sc, name, true, 1, types);
Sean Callanan9394b5a2011-10-29 19:50:43 +0000597
598 if (types.GetSize())
599 {
600 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
601
602 if (log)
603 {
604 const char *name_string = type_sp->GetName().GetCString();
605
606 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
607 current_id,
608 name.GetCString(),
609 (name_string ? name_string : "<anonymous>"));
610 }
611
Sean Callanane1301a62011-12-06 03:41:14 +0000612
Sean Callanan9394b5a2011-10-29 19:50:43 +0000613 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
Sean Callanane1301a62011-12-06 03:41:14 +0000614
Sean Callanandc5fce12011-12-01 21:04:37 +0000615 if (!copied_type)
616 {
617 if (log)
Sean Callanane1301a62011-12-06 03:41:14 +0000618 log->Printf(" CAS::FEVD[%u] - Couldn't export the type for a constant integer result",
619 current_id);
620
Sean Callanandc5fce12011-12-01 21:04:37 +0000621 break;
622 }
623
Sean Callanan9394b5a2011-10-29 19:50:43 +0000624 context.AddTypeDecl(copied_type);
625 }
Sean Callanan673f3db2011-11-30 22:11:59 +0000626
Sean Callanan9394b5a2011-10-29 19:50:43 +0000627 } while(0);
628}
629
Sean Callanan9b714842011-11-09 19:33:21 +0000630void
631ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
632{
633 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
634
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000635 static unsigned int invocation_id = 0;
636 unsigned int current_id = invocation_id++;
637
Sean Callanan9b714842011-11-09 19:33:21 +0000638 const DeclarationName &decl_name(context.m_decl_name);
639 const DeclContext *decl_ctx(context.m_decl_context);
640
641 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
642
643 if (!interface_decl)
644 return;
645
646 StreamString ss;
Sean Callanane1301a62011-12-06 03:41:14 +0000647
Sean Callanan9b714842011-11-09 19:33:21 +0000648 if (decl_name.isObjCZeroArgSelector())
649 {
650 ss.Printf("%s", decl_name.getAsString().c_str());
651 }
652 else if (decl_name.isObjCOneArgSelector())
653 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000654 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000655 }
656 else
657 {
658 clang::Selector sel = decl_name.getObjCSelector();
659
660 for (unsigned i = 0, e = sel.getNumArgs();
661 i != e;
662 ++i)
663 {
664 llvm::StringRef r = sel.getNameForSlot(i);
665 ss.Printf("%s:", r.str().c_str());
666 }
667 }
668 ss.Flush();
669
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000670 ConstString selector_name(ss.GetData());
671
Sean Callanan9b714842011-11-09 19:33:21 +0000672 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000673 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
674 current_id,
675 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000676 interface_decl->getNameAsString().c_str(),
677 selector_name.AsCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000678 SymbolContextList sc_list;
679
680 const bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000681 const bool include_inlines = false;
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000682 const bool append = false;
683
Sean Callanane0028b82012-01-19 02:17:40 +0000684 std::string interface_name = interface_decl->getNameAsString();
685
686 do
687 {
688 StreamString ms;
689 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
690 ms.Flush();
691 ConstString instance_method_name(ms.GetData());
692
Sean Callanan302d78c2012-02-10 22:52:19 +0000693 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000694
695 if (sc_list.GetSize())
696 break;
697
698 ms.Clear();
699 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
700 ms.Flush();
701 ConstString class_method_name(ms.GetData());
702
Sean Callanan302d78c2012-02-10 22:52:19 +0000703 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000704
705 if (sc_list.GetSize())
706 break;
707
708 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
709 // categories on the desired class.
710
711 SymbolContextList candidate_sc_list;
712
Sean Callanan302d78c2012-02-10 22:52:19 +0000713 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000714
715 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
716 ci != ce;
717 ++ci)
718 {
719 SymbolContext candidate_sc;
720
721 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
722 continue;
723
724 if (!candidate_sc.function)
725 continue;
726
727 const char *candidate_name = candidate_sc.function->GetName().AsCString();
728
729 const char *cursor = candidate_name;
730
731 if (*cursor != '+' && *cursor != '-')
732 continue;
733
734 ++cursor;
735
736 if (*cursor != '[')
737 continue;
738
739 ++cursor;
740
741 size_t interface_len = interface_name.length();
742
743 if (strncmp(cursor, interface_name.c_str(), interface_len))
744 continue;
745
746 cursor += interface_len;
747
748 if (*cursor == ' ' || *cursor == '(')
749 sc_list.Append(candidate_sc);
750 }
751 }
752 while (0);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000753
754 for (uint32_t i = 0, e = sc_list.GetSize();
755 i != e;
756 ++i)
757 {
758 SymbolContext sc;
759
760 if (!sc_list.GetContextAtIndex(i, sc))
761 continue;
762
763 if (!sc.function)
764 continue;
765
766 DeclContext *function_ctx = sc.function->GetClangDeclContext();
767
768 if (!function_ctx)
769 continue;
770
771 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
772
773 if (!method_decl)
774 continue;
775
776 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
777
778 if (!found_interface_decl)
779 continue;
780
781 if (found_interface_decl->getName() == interface_decl->getName())
782 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000783 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000784
785 if (!copied_decl)
786 continue;
787
788 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
789
790 if (!copied_method_decl)
791 continue;
792
793 if (log)
794 {
795 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanane1301a62011-12-06 03:41:14 +0000796 log->Printf(" CAS::FOMD[%d] found (in debug info) %s", current_id, dumper.GetCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000797 }
Sean Callanane0028b82012-01-19 02:17:40 +0000798
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000799 context.AddNamedDecl(copied_method_decl);
800 }
801 }
Sean Callanan9b714842011-11-09 19:33:21 +0000802}
803
Sean Callanan8f2e3922012-02-04 08:49:35 +0000804template <class D> class TaggedASTDecl {
805public:
806 TaggedASTDecl() : decl(NULL) { }
807 TaggedASTDecl(D *_decl) : decl(_decl) { }
808 bool IsValid() const { return (decl != NULL); }
809 bool IsInvalid() const { return !IsValid(); }
810 D *operator->() const { return decl; }
811 D *decl;
812};
813
814template <class D2, template <class D> class TD, class D1>
815TD<D2>
816DynCast(TD<D1> source)
817{
818 return TD<D2> (dyn_cast<D2>(source.decl));
819}
820
821template <class D = Decl> class DeclFromParser;
822template <class D = Decl> class DeclFromUser;
823
824template <class D> class DeclFromParser : public TaggedASTDecl<D> {
825public:
826 DeclFromParser() : TaggedASTDecl<D>() { }
827 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
828
829 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
830};
831
832template <class D> class DeclFromUser : public TaggedASTDecl<D> {
833public:
834 DeclFromUser() : TaggedASTDecl<D>() { }
835 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
836
837 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
838};
839
840template <class D>
841DeclFromUser<D>
842DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
843{
844 DeclFromUser <> origin_decl;
845 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
846 if (origin_decl.IsInvalid())
847 return DeclFromUser<D>();
848 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
849}
850
851template <class D>
852DeclFromParser<D>
853DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
854{
855 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
856 if (parser_generic_decl.IsInvalid())
857 return DeclFromParser<D>();
858 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
859}
860
Sean Callanan931acec2012-02-22 23:57:45 +0000861static bool
862FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
863 NameSearchContext &context,
864 clang::ASTContext &ast_context,
865 ClangASTImporter *ast_importer,
866 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
867{
868 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
869
870 if (origin_iface_decl.IsInvalid())
871 return false;
872
873 std::string name_str = context.m_decl_name.getAsString();
874 StringRef name(name_str.c_str());
875 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
876
877 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
878
879 bool found = false;
880
881 if (origin_property_decl.IsValid())
882 {
883 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
884 if (parser_property_decl.IsValid())
885 {
886 if (log)
887 {
888 ASTDumper dumper((Decl*)parser_property_decl.decl);
889 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
890 }
891
892 context.AddNamedDecl(parser_property_decl.decl);
893 found = true;
894 }
895 }
896
897 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
898
899 if (origin_ivar_decl.IsValid())
900 {
901 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
902 if (parser_ivar_decl.IsValid())
903 {
904 if (log)
905 {
906 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
907 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
908 }
909
910 context.AddNamedDecl(parser_ivar_decl.decl);
911 found = true;
912 }
913 }
914
915 return found;
916}
917
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000918void
Sean Callanan8f2e3922012-02-04 08:49:35 +0000919ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000920{
921 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
922
923 static unsigned int invocation_id = 0;
924 unsigned int current_id = invocation_id++;
925
Sean Callanan8f2e3922012-02-04 08:49:35 +0000926 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
927 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan931acec2012-02-22 23:57:45 +0000928
929 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000930
931 if (log)
Sean Callanan8f2e3922012-02-04 08:49:35 +0000932 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000933 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000934 m_ast_context,
Sean Callanan8f2e3922012-02-04 08:49:35 +0000935 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan931acec2012-02-22 23:57:45 +0000936 context.m_decl_name.getAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000937
Sean Callanan931acec2012-02-22 23:57:45 +0000938 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
939 context,
940 *m_ast_context,
941 m_ast_importer,
942 origin_iface_decl))
943 return;
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000944
Sean Callanan931acec2012-02-22 23:57:45 +0000945 if (log)
946 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
947 current_id,
948 origin_iface_decl.decl,
949 &origin_iface_decl->getASTContext());
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000950
Sean Callanan931acec2012-02-22 23:57:45 +0000951 SymbolContext null_sc;
952 TypeList type_list;
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000953
Sean Callanan931acec2012-02-22 23:57:45 +0000954 lldb::ProcessSP process(m_target->GetProcessSP());
955
956 if (!process)
957 return;
958
959 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
960
961 if (!language_runtime)
962 return;
963
964 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
965
966 if (!complete_type_sp)
967 return;
968
969 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType(), complete_type_sp->GetClangAST());
970 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
971
972 if (!complete_opaque_type)
973 return;
974
975 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
976 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
977
978 if (!complete_interface_type)
979 return;
980
981 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_type->getDecl());
982
983 if (complete_iface_decl.decl == origin_iface_decl.decl)
984 return; // already checked this one
985
986 if (log)
987 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
988 current_id,
989 complete_iface_decl.decl,
990 &complete_iface_decl->getASTContext());
991
992
993 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
994 context,
995 *m_ast_context,
996 m_ast_importer,
997 complete_iface_decl))
998 return;
Sean Callanan8f2e3922012-02-04 08:49:35 +0000999}
1000
1001typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1002typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1003
1004template <class D, class O>
1005static bool
1006ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1007 llvm::DenseMap <const D*, O> &source_map,
1008 ClangASTImporter *importer,
1009 ASTContext &dest_ctx)
1010{
1011 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001012
Sean Callanan8f2e3922012-02-04 08:49:35 +00001013 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1014 fi != fe;
1015 ++fi)
1016 {
1017 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1018 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1019 if (parser_decl.IsInvalid())
1020 return false;
1021 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1022 }
1023
1024 return true;
1025}
1026
1027template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1028 DeclFromUser<const CXXRecordDecl> &record,
1029 BaseOffsetMap &base_offsets)
1030{
1031 for (CXXRecordDecl::base_class_const_iterator
1032 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1033 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1034 bi != be;
1035 ++bi)
1036 {
1037 if (!IsVirtual && bi->isVirtual())
1038 continue;
1039
1040 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1041 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1042
1043 if (!origin_base_record_type)
1044 return false;
1045
1046 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1047
1048 if (origin_base_record.IsInvalid())
1049 return false;
1050
1051 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1052
1053 if (origin_base_cxx_record.IsInvalid())
1054 return false;
1055
1056 CharUnits base_offset;
1057
1058 if (IsVirtual)
1059 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1060 else
1061 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1062
1063 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1064 }
1065
1066 return true;
1067}
1068
1069bool
1070ClangASTSource::layoutRecordType(const RecordDecl *record,
1071 uint64_t &size,
1072 uint64_t &alignment,
1073 FieldOffsetMap &field_offsets,
1074 BaseOffsetMap &base_offsets,
1075 BaseOffsetMap &virtual_base_offsets)
1076{
1077 static unsigned int invocation_id = 0;
1078 unsigned int current_id = invocation_id++;
1079
1080 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1081
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001082 if (log)
1083 {
Sean Callanan8f2e3922012-02-04 08:49:35 +00001084 log->Printf("LayoutRecordType[%u] on (RecordDecl*)%p [name = '%s']",
1085 current_id,
1086 m_ast_context,
1087 record->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001088 }
Sean Callanan8f2e3922012-02-04 08:49:35 +00001089
1090
1091 DeclFromParser <const RecordDecl> parser_record(record);
1092 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1093
1094 if (origin_record.IsInvalid())
1095 return false;
1096
1097 FieldOffsetMap origin_field_offsets;
1098 BaseOffsetMap origin_base_offsets;
1099 BaseOffsetMap origin_virtual_base_offsets;
1100
1101 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1102
1103 int field_idx = 0;
1104
1105 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1106 fi != fe;
1107 ++fi)
1108 {
1109 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1110
1111 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1112
1113 field_idx++;
1114 }
1115
1116 ASTContext &parser_ast_context(record->getASTContext());
1117
1118 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1119
1120 if (origin_cxx_record.IsValid())
1121 {
1122 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1123 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1124 return false;
1125 }
1126
1127 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1128 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1129 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1130 return false;
1131
1132 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1133 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1134
1135 if (log)
1136 {
1137 log->Printf("LRT[%u] returned:", current_id);
1138 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
1139 log->Printf("LRT[%u] Size = %lld", current_id, size);
1140 log->Printf("LRT[%u] Alignment = %lld", current_id, alignment);
1141 log->Printf("LRT[%u] Fields:", current_id);
1142 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1143 fi != fe;
1144 ++fi)
1145 {
1146 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %lld bits",
1147 current_id,
1148 *fi,
1149 fi->getNameAsString().c_str(),
1150 field_offsets[*fi]);
1151 }
1152 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1153 if (parser_cxx_record.IsValid())
1154 {
1155 log->Printf("LRT[%u] Bases:", current_id);
1156 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1157 bi != be;
1158 ++bi)
1159 {
1160 bool is_virtual = bi->isVirtual();
1161
1162 QualType base_type = bi->getType();
1163 const RecordType *base_record_type = base_type->getAs<RecordType>();
1164 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1165 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1166
1167 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %lld chars",
1168 current_id,
1169 (is_virtual ? "Virtual " : ""),
1170 base_cxx_record.decl,
1171 base_cxx_record.decl->getNameAsString().c_str(),
1172 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1173 base_offsets[base_cxx_record.decl].getQuantity()));
1174 }
1175 }
1176 else
1177 {
1178 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1179 }
1180 }
1181
1182 return true;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001183}
1184
Sean Callanan73b520f2011-10-29 01:58:46 +00001185void
1186ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1187 const ConstString &name,
1188 ClangASTImporter::NamespaceMapSP &parent_map) const
1189{
1190 static unsigned int invocation_id = 0;
1191 unsigned int current_id = invocation_id++;
1192
1193 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1194
1195 if (log)
1196 {
1197 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001198 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001199 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001200 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001201 name.GetCString(),
1202 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1203 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001204 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001205 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001206 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001207 name.GetCString());
1208 }
1209
1210
1211 if (parent_map)
1212 {
1213 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1214 i != e;
1215 ++i)
1216 {
1217 ClangNamespaceDecl found_namespace_decl;
1218
1219 lldb::ModuleSP module_sp = i->first;
1220 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1221
1222 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1223
1224 if (!symbol_vendor)
1225 continue;
1226
1227 SymbolContext null_sc;
1228
1229 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1230
1231 if (!found_namespace_decl)
1232 continue;
1233
1234 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1235
1236 if (log)
1237 log->Printf(" CMN[%u] Found namespace %s in module %s",
1238 current_id,
1239 name.GetCString(),
1240 module_sp->GetFileSpec().GetFilename().GetCString());
1241 }
1242 }
1243 else
1244 {
1245 ModuleList &images = m_target->GetImages();
1246 ClangNamespaceDecl null_namespace_decl;
1247
1248 for (uint32_t i = 0, e = images.GetSize();
1249 i != e;
1250 ++i)
1251 {
1252 lldb::ModuleSP image = images.GetModuleAtIndex(i);
1253
1254 if (!image)
1255 continue;
1256
1257 ClangNamespaceDecl found_namespace_decl;
1258
1259 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1260
1261 if (!symbol_vendor)
1262 continue;
1263
1264 SymbolContext null_sc;
1265
1266 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1267
1268 if (!found_namespace_decl)
1269 continue;
1270
1271 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1272
1273 if (log)
1274 log->Printf(" CMN[%u] Found namespace %s in module %s",
1275 current_id,
1276 name.GetCString(),
1277 image->GetFileSpec().GetFilename().GetCString());
1278 }
1279 }
1280}
1281
Sean Callananbb715f92011-10-29 02:28:18 +00001282NamespaceDecl *
1283ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1284{
Greg Clayton13d24fb2012-01-29 20:56:30 +00001285 if (!namespace_decls)
Sean Callananbb715f92011-10-29 02:28:18 +00001286 return NULL;
1287
1288 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1289
1290 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1291
Sean Callanan4938bd62011-11-16 18:20:47 +00001292 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +00001293
1294 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1295
1296 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1297
1298 return dyn_cast<NamespaceDecl>(copied_decl);
1299}
1300
Sean Callanan9394b5a2011-10-29 19:50:43 +00001301void *
1302ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1303 ASTContext *source_context,
1304 void *clang_type)
1305{
1306 SetImportInProgress(true);
1307
Sean Callanan4938bd62011-11-16 18:20:47 +00001308 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +00001309
1310 void *ret = ret_qual_type.getAsOpaquePtr();
1311
1312 SetImportInProgress(false);
1313
Sean Callananaa11af82012-02-27 19:22:39 +00001314 if (ret_qual_type->getCanonicalTypeInternal().isNull())
1315 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1316 // on occasion.
1317 return NULL;
1318
Sean Callanan9394b5a2011-10-29 19:50:43 +00001319 return ret;
1320}
1321
Greg Claytonb01000f2011-01-17 03:46:26 +00001322clang::NamedDecl *
1323NameSearchContext::AddVarDecl(void *type)
1324{
Greg Clayton8de27c72010-10-15 22:48:33 +00001325 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +00001326
1327 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +00001328
Sean Callananf76afff2011-10-28 23:38:38 +00001329 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001330 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +00001331 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001332 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +00001333 ii,
Chris Lattner24943d22010-06-08 16:52:24 +00001334 QualType::getFromOpaquePtr(type),
1335 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001336 SC_Static,
1337 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +00001338 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +00001339
1340 return Decl;
1341}
Sean Callanan8f0dc342010-06-22 23:46:24 +00001342
Greg Claytonb01000f2011-01-17 03:46:26 +00001343clang::NamedDecl *
1344NameSearchContext::AddFunDecl (void *type)
1345{
Sean Callananf76afff2011-10-28 23:38:38 +00001346 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001347 const_cast<DeclContext*>(m_decl_context),
1348 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001349 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001350 m_decl_name.getAsIdentifierInfo(),
1351 QualType::getFromOpaquePtr(type),
1352 NULL,
1353 SC_Static,
1354 SC_Static,
1355 false,
1356 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001357
Sean Callananb291abe2010-08-12 23:45:38 +00001358 // We have to do more than just synthesize the FunctionDecl. We have to
1359 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1360 // this, we raid the function's FunctionProtoType for types.
1361
Greg Clayton8de27c72010-10-15 22:48:33 +00001362 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callanan21f2e192011-12-14 01:13:04 +00001363 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001364
Greg Clayton8de27c72010-10-15 22:48:33 +00001365 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +00001366 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001367 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001368 unsigned ArgIndex;
1369
Sean Callananc1535182011-10-07 23:18:13 +00001370 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1371
Sean Callanan8f0dc342010-06-22 23:46:24 +00001372 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1373 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001374 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001375
Sean Callananf76afff2011-10-28 23:38:38 +00001376 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +00001377 const_cast<DeclContext*>(m_decl_context),
1378 SourceLocation(),
1379 SourceLocation(),
1380 NULL,
1381 arg_qual_type,
1382 NULL,
1383 SC_Static,
1384 SC_Static,
1385 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001386 }
1387
Sean Callananc1535182011-10-07 23:18:13 +00001388 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001389 }
Sean Callanandc5fce12011-12-01 21:04:37 +00001390 else
1391 {
1392 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1393
1394 log->Printf("Function type wasn't a FunctionProtoType");
1395 }
Sean Callanan8f0dc342010-06-22 23:46:24 +00001396
Greg Clayton8de27c72010-10-15 22:48:33 +00001397 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001398
Greg Clayton8de27c72010-10-15 22:48:33 +00001399 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +00001400}
Sean Callanan0fc73582010-07-27 00:55:47 +00001401
Greg Claytonb01000f2011-01-17 03:46:26 +00001402clang::NamedDecl *
1403NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +00001404{
Sean Callananad293092011-01-18 23:32:05 +00001405 FunctionProtoType::ExtProtoInfo proto_info;
1406
1407 proto_info.Variadic = true;
1408
Sean Callananf76afff2011-10-28 23:38:38 +00001409 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1410 NULL, // argument types
1411 0, // number of arguments
1412 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +00001413
Sean Callanan0fc73582010-07-27 00:55:47 +00001414 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1415}
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001416
Greg Claytonb01000f2011-01-17 03:46:26 +00001417clang::NamedDecl *
1418NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001419{
Greg Claytona1aaaff2011-01-23 00:34:52 +00001420 if (type)
1421 {
1422 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001423
Sean Callanan21f2e192011-12-14 01:13:04 +00001424 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001425 {
1426 TagDecl *tag_decl = tag_type->getDecl();
1427
1428 m_decls.push_back(tag_decl);
1429
1430 return tag_decl;
1431 }
Sean Callanan21f2e192011-12-14 01:13:04 +00001432 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001433 {
1434 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1435
1436 m_decls.push_back((NamedDecl*)interface_decl);
1437
1438 return (NamedDecl*)interface_decl;
1439 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001440 }
Greg Claytona1aaaff2011-01-23 00:34:52 +00001441 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001442}
Greg Claytone6d72ca2011-06-25 00:44:06 +00001443
1444void
1445NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1446{
1447 for (clang::NamedDecl * const *decl_iterator = result.first;
1448 decl_iterator != result.second;
1449 ++decl_iterator)
1450 m_decls.push_back (*decl_iterator);
1451}
1452
1453void
1454NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1455{
1456 m_decls.push_back (decl);
1457}