blob: d50e80854e218393730ee4e5d1825f9eea2477fe [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 Callananddb59332012-03-20 21:11:12 +0000390 if (!copied_decl)
391 continue;
392
Sean Callanane9478392012-03-15 01:53:17 +0000393 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
394 {
395 QualType copied_field_type = copied_field->getType();
396
397 if (const TagType *copied_field_tag_type = copied_field_type->getAs<TagType>())
398 m_ast_importer->CompleteTagDecl(copied_field_tag_type->getDecl());
399 if (const ObjCObjectType *copied_field_object_type = copied_field_type->getAs<ObjCObjectType>())
400 if (ObjCInterfaceDecl *copied_field_objc_interface_decl = copied_field_object_type->getInterface())
401 m_ast_importer->CompleteObjCInterfaceDecl(copied_field_objc_interface_decl);
402 }
403
Sean Callananbb715f92011-10-29 02:28:18 +0000404 decls.push_back(copied_decl);
405 }
406 }
407
408 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000409}
410
Sean Callanan9394b5a2011-10-29 19:50:43 +0000411void
412ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
413{
414 assert (m_ast_context);
415
416 const ConstString name(context.m_decl_name.getAsString().c_str());
417
418 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
419
420 static unsigned int invocation_id = 0;
421 unsigned int current_id = invocation_id++;
422
423 if (log)
424 {
425 if (!context.m_decl_context)
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000426 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 +0000427 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000428 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 +0000429 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000430 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 +0000431 }
432
433 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
434
435 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
436 {
437 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
438
439 if (log && log->GetVerbose())
440 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
441 current_id,
442 namespace_map.get(),
443 (int)namespace_map->size());
444
445 if (!namespace_map)
446 return;
447
448 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
449 i != e;
450 ++i)
451 {
452 if (log)
453 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
454 current_id,
455 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
456 i->first->GetFileSpec().GetFilename().GetCString());
457
458 FindExternalVisibleDecls(context,
459 i->first,
460 i->second,
461 current_id);
462 }
463 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000464 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000465 {
Sean Callanan8f2e3922012-02-04 08:49:35 +0000466 FindObjCPropertyAndIvarDecls(context);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000467 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000468 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
469 {
470 // we shouldn't be getting FindExternalVisibleDecls calls for these
471 return;
472 }
473 else
474 {
475 ClangNamespaceDecl namespace_decl;
476
477 if (log)
478 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
479
480 FindExternalVisibleDecls(context,
481 lldb::ModuleSP(),
482 namespace_decl,
483 current_id);
484 }
485
486 if (!context.m_namespace_map->empty())
487 {
488 if (log && log->GetVerbose())
489 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
490 current_id,
491 context.m_namespace_map.get(),
492 (int)context.m_namespace_map->size());
493
494 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
495
496 if (clang_namespace_decl)
497 clang_namespace_decl->setHasExternalVisibleStorage();
498 }
499}
500
501void
502ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
503 lldb::ModuleSP module_sp,
504 ClangNamespaceDecl &namespace_decl,
505 unsigned int current_id)
506{
507 assert (m_ast_context);
508
509 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
510
511 SymbolContextList sc_list;
512
513 const ConstString name(context.m_decl_name.getAsString().c_str());
514
515 const char *name_unique_cstr = name.GetCString();
516
Sean Callanan8f2e3922012-02-04 08:49:35 +0000517 static ConstString id_name("id");
518 static ConstString Class_name("Class");
519
520 if (name == id_name || name == Class_name)
521 return;
522
Sean Callanan9394b5a2011-10-29 19:50:43 +0000523 if (name_unique_cstr == NULL)
524 return;
525
526 // The ClangASTSource is not responsible for finding $-names.
527 if (name_unique_cstr[0] == '$')
528 return;
529
530 if (module_sp && namespace_decl)
531 {
532 ClangNamespaceDecl found_namespace_decl;
533
534 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
535
536 if (symbol_vendor)
537 {
538 SymbolContext null_sc;
539
540 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
541
542 if (found_namespace_decl)
543 {
544 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
545
546 if (log)
547 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
548 current_id,
549 name.GetCString(),
550 module_sp->GetFileSpec().GetFilename().GetCString());
551 }
552 }
553 }
554 else
555 {
556 ModuleList &images = m_target->GetImages();
557
558 for (uint32_t i = 0, e = images.GetSize();
559 i != e;
560 ++i)
561 {
562 lldb::ModuleSP image = images.GetModuleAtIndex(i);
563
564 if (!image)
565 continue;
566
567 ClangNamespaceDecl found_namespace_decl;
568
569 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
570
571 if (!symbol_vendor)
572 continue;
573
574 SymbolContext null_sc;
575
576 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
577
578 if (found_namespace_decl)
579 {
580 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
581
582 if (log)
583 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
584 current_id,
585 name.GetCString(),
586 image->GetFileSpec().GetFilename().GetCString());
587 }
588 }
589 }
590
Sean Callanan9394b5a2011-10-29 19:50:43 +0000591 do
592 {
593 TypeList types;
594 SymbolContext null_sc;
Sean Callanan0f71d192011-12-19 19:38:39 +0000595
Sean Callanan9394b5a2011-10-29 19:50:43 +0000596 if (module_sp && namespace_decl)
597 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
Sean Callanan0f71d192011-12-19 19:38:39 +0000598 else
Sean Callanane1301a62011-12-06 03:41:14 +0000599 m_target->GetImages().FindTypes(null_sc, name, true, 1, types);
Sean Callanan9394b5a2011-10-29 19:50:43 +0000600
601 if (types.GetSize())
602 {
603 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
604
605 if (log)
606 {
607 const char *name_string = type_sp->GetName().GetCString();
608
609 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
610 current_id,
611 name.GetCString(),
612 (name_string ? name_string : "<anonymous>"));
613 }
614
Sean Callanane1301a62011-12-06 03:41:14 +0000615
Sean Callanan9394b5a2011-10-29 19:50:43 +0000616 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
Sean Callanane1301a62011-12-06 03:41:14 +0000617
Sean Callanandc5fce12011-12-01 21:04:37 +0000618 if (!copied_type)
619 {
620 if (log)
Sean Callanane1301a62011-12-06 03:41:14 +0000621 log->Printf(" CAS::FEVD[%u] - Couldn't export the type for a constant integer result",
622 current_id);
623
Sean Callanandc5fce12011-12-01 21:04:37 +0000624 break;
625 }
626
Sean Callanan9394b5a2011-10-29 19:50:43 +0000627 context.AddTypeDecl(copied_type);
628 }
Sean Callanan673f3db2011-11-30 22:11:59 +0000629
Sean Callanan9394b5a2011-10-29 19:50:43 +0000630 } while(0);
631}
632
Sean Callanan9b714842011-11-09 19:33:21 +0000633void
634ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
635{
636 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
637
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000638 static unsigned int invocation_id = 0;
639 unsigned int current_id = invocation_id++;
640
Sean Callanan9b714842011-11-09 19:33:21 +0000641 const DeclarationName &decl_name(context.m_decl_name);
642 const DeclContext *decl_ctx(context.m_decl_context);
643
644 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
645
646 if (!interface_decl)
647 return;
648
649 StreamString ss;
Sean Callanane1301a62011-12-06 03:41:14 +0000650
Sean Callanan9b714842011-11-09 19:33:21 +0000651 if (decl_name.isObjCZeroArgSelector())
652 {
653 ss.Printf("%s", decl_name.getAsString().c_str());
654 }
655 else if (decl_name.isObjCOneArgSelector())
656 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000657 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000658 }
659 else
660 {
661 clang::Selector sel = decl_name.getObjCSelector();
662
663 for (unsigned i = 0, e = sel.getNumArgs();
664 i != e;
665 ++i)
666 {
667 llvm::StringRef r = sel.getNameForSlot(i);
668 ss.Printf("%s:", r.str().c_str());
669 }
670 }
671 ss.Flush();
672
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000673 ConstString selector_name(ss.GetData());
674
Sean Callanan9b714842011-11-09 19:33:21 +0000675 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000676 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
677 current_id,
678 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000679 interface_decl->getNameAsString().c_str(),
680 selector_name.AsCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000681 SymbolContextList sc_list;
682
683 const bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000684 const bool include_inlines = false;
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000685 const bool append = false;
686
Sean Callanane0028b82012-01-19 02:17:40 +0000687 std::string interface_name = interface_decl->getNameAsString();
688
689 do
690 {
691 StreamString ms;
692 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
693 ms.Flush();
694 ConstString instance_method_name(ms.GetData());
695
Sean Callanan302d78c2012-02-10 22:52:19 +0000696 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000697
698 if (sc_list.GetSize())
699 break;
700
701 ms.Clear();
702 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
703 ms.Flush();
704 ConstString class_method_name(ms.GetData());
705
Sean Callanan302d78c2012-02-10 22:52:19 +0000706 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000707
708 if (sc_list.GetSize())
709 break;
710
711 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
712 // categories on the desired class.
713
714 SymbolContextList candidate_sc_list;
715
Sean Callanan302d78c2012-02-10 22:52:19 +0000716 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000717
718 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
719 ci != ce;
720 ++ci)
721 {
722 SymbolContext candidate_sc;
723
724 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
725 continue;
726
727 if (!candidate_sc.function)
728 continue;
729
730 const char *candidate_name = candidate_sc.function->GetName().AsCString();
731
732 const char *cursor = candidate_name;
733
734 if (*cursor != '+' && *cursor != '-')
735 continue;
736
737 ++cursor;
738
739 if (*cursor != '[')
740 continue;
741
742 ++cursor;
743
744 size_t interface_len = interface_name.length();
745
746 if (strncmp(cursor, interface_name.c_str(), interface_len))
747 continue;
748
749 cursor += interface_len;
750
751 if (*cursor == ' ' || *cursor == '(')
752 sc_list.Append(candidate_sc);
753 }
754 }
755 while (0);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000756
757 for (uint32_t i = 0, e = sc_list.GetSize();
758 i != e;
759 ++i)
760 {
761 SymbolContext sc;
762
763 if (!sc_list.GetContextAtIndex(i, sc))
764 continue;
765
766 if (!sc.function)
767 continue;
768
769 DeclContext *function_ctx = sc.function->GetClangDeclContext();
770
771 if (!function_ctx)
772 continue;
773
774 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
775
776 if (!method_decl)
777 continue;
778
779 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
780
781 if (!found_interface_decl)
782 continue;
783
784 if (found_interface_decl->getName() == interface_decl->getName())
785 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000786 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000787
788 if (!copied_decl)
789 continue;
790
791 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
792
793 if (!copied_method_decl)
794 continue;
795
796 if (log)
797 {
798 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanane1301a62011-12-06 03:41:14 +0000799 log->Printf(" CAS::FOMD[%d] found (in debug info) %s", current_id, dumper.GetCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000800 }
Sean Callanane0028b82012-01-19 02:17:40 +0000801
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000802 context.AddNamedDecl(copied_method_decl);
803 }
804 }
Sean Callanan9b714842011-11-09 19:33:21 +0000805}
806
Sean Callanan8f2e3922012-02-04 08:49:35 +0000807template <class D> class TaggedASTDecl {
808public:
809 TaggedASTDecl() : decl(NULL) { }
810 TaggedASTDecl(D *_decl) : decl(_decl) { }
811 bool IsValid() const { return (decl != NULL); }
812 bool IsInvalid() const { return !IsValid(); }
813 D *operator->() const { return decl; }
814 D *decl;
815};
816
817template <class D2, template <class D> class TD, class D1>
818TD<D2>
819DynCast(TD<D1> source)
820{
821 return TD<D2> (dyn_cast<D2>(source.decl));
822}
823
824template <class D = Decl> class DeclFromParser;
825template <class D = Decl> class DeclFromUser;
826
827template <class D> class DeclFromParser : public TaggedASTDecl<D> {
828public:
829 DeclFromParser() : TaggedASTDecl<D>() { }
830 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
831
832 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
833};
834
835template <class D> class DeclFromUser : public TaggedASTDecl<D> {
836public:
837 DeclFromUser() : TaggedASTDecl<D>() { }
838 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
839
840 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
841};
842
843template <class D>
844DeclFromUser<D>
845DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
846{
847 DeclFromUser <> origin_decl;
848 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
849 if (origin_decl.IsInvalid())
850 return DeclFromUser<D>();
851 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
852}
853
854template <class D>
855DeclFromParser<D>
856DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
857{
858 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
859 if (parser_generic_decl.IsInvalid())
860 return DeclFromParser<D>();
861 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
862}
863
Sean Callanan931acec2012-02-22 23:57:45 +0000864static bool
865FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
866 NameSearchContext &context,
867 clang::ASTContext &ast_context,
868 ClangASTImporter *ast_importer,
869 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
870{
871 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
872
873 if (origin_iface_decl.IsInvalid())
874 return false;
875
876 std::string name_str = context.m_decl_name.getAsString();
877 StringRef name(name_str.c_str());
878 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
879
880 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
881
882 bool found = false;
883
884 if (origin_property_decl.IsValid())
885 {
886 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
887 if (parser_property_decl.IsValid())
888 {
889 if (log)
890 {
891 ASTDumper dumper((Decl*)parser_property_decl.decl);
892 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
893 }
894
895 context.AddNamedDecl(parser_property_decl.decl);
896 found = true;
897 }
898 }
899
900 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
901
902 if (origin_ivar_decl.IsValid())
903 {
904 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
905 if (parser_ivar_decl.IsValid())
906 {
907 if (log)
908 {
909 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
910 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
911 }
912
913 context.AddNamedDecl(parser_ivar_decl.decl);
914 found = true;
915 }
916 }
917
918 return found;
919}
920
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000921void
Sean Callanan8f2e3922012-02-04 08:49:35 +0000922ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000923{
924 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
925
926 static unsigned int invocation_id = 0;
927 unsigned int current_id = invocation_id++;
928
Sean Callanan8f2e3922012-02-04 08:49:35 +0000929 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
930 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan931acec2012-02-22 23:57:45 +0000931
932 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000933
934 if (log)
Sean Callanan8f2e3922012-02-04 08:49:35 +0000935 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000936 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000937 m_ast_context,
Sean Callanan8f2e3922012-02-04 08:49:35 +0000938 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan931acec2012-02-22 23:57:45 +0000939 context.m_decl_name.getAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000940
Sean Callanan931acec2012-02-22 23:57:45 +0000941 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
942 context,
943 *m_ast_context,
944 m_ast_importer,
945 origin_iface_decl))
946 return;
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000947
Sean Callanan931acec2012-02-22 23:57:45 +0000948 if (log)
949 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
950 current_id,
951 origin_iface_decl.decl,
952 &origin_iface_decl->getASTContext());
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000953
Sean Callanan931acec2012-02-22 23:57:45 +0000954 SymbolContext null_sc;
955 TypeList type_list;
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000956
Sean Callanan931acec2012-02-22 23:57:45 +0000957 lldb::ProcessSP process(m_target->GetProcessSP());
958
959 if (!process)
960 return;
961
962 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
963
964 if (!language_runtime)
965 return;
966
967 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
968
969 if (!complete_type_sp)
970 return;
971
972 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType(), complete_type_sp->GetClangAST());
973 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
974
975 if (!complete_opaque_type)
976 return;
977
978 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
979 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
980
981 if (!complete_interface_type)
982 return;
983
984 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_type->getDecl());
985
986 if (complete_iface_decl.decl == origin_iface_decl.decl)
987 return; // already checked this one
988
989 if (log)
990 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
991 current_id,
992 complete_iface_decl.decl,
993 &complete_iface_decl->getASTContext());
994
995
996 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
997 context,
998 *m_ast_context,
999 m_ast_importer,
1000 complete_iface_decl))
1001 return;
Sean Callanan8f2e3922012-02-04 08:49:35 +00001002}
1003
1004typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1005typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1006
1007template <class D, class O>
1008static bool
1009ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1010 llvm::DenseMap <const D*, O> &source_map,
1011 ClangASTImporter *importer,
1012 ASTContext &dest_ctx)
1013{
1014 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001015
Sean Callanan8f2e3922012-02-04 08:49:35 +00001016 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1017 fi != fe;
1018 ++fi)
1019 {
1020 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1021 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1022 if (parser_decl.IsInvalid())
1023 return false;
1024 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1025 }
1026
1027 return true;
1028}
1029
1030template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1031 DeclFromUser<const CXXRecordDecl> &record,
1032 BaseOffsetMap &base_offsets)
1033{
1034 for (CXXRecordDecl::base_class_const_iterator
1035 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1036 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1037 bi != be;
1038 ++bi)
1039 {
1040 if (!IsVirtual && bi->isVirtual())
1041 continue;
1042
1043 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1044 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1045
1046 if (!origin_base_record_type)
1047 return false;
1048
1049 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1050
1051 if (origin_base_record.IsInvalid())
1052 return false;
1053
1054 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1055
1056 if (origin_base_cxx_record.IsInvalid())
1057 return false;
1058
1059 CharUnits base_offset;
1060
1061 if (IsVirtual)
1062 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1063 else
1064 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1065
1066 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1067 }
1068
1069 return true;
1070}
1071
1072bool
1073ClangASTSource::layoutRecordType(const RecordDecl *record,
1074 uint64_t &size,
1075 uint64_t &alignment,
1076 FieldOffsetMap &field_offsets,
1077 BaseOffsetMap &base_offsets,
1078 BaseOffsetMap &virtual_base_offsets)
1079{
1080 static unsigned int invocation_id = 0;
1081 unsigned int current_id = invocation_id++;
1082
1083 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1084
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001085 if (log)
1086 {
Sean Callanan8f2e3922012-02-04 08:49:35 +00001087 log->Printf("LayoutRecordType[%u] on (RecordDecl*)%p [name = '%s']",
1088 current_id,
1089 m_ast_context,
1090 record->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001091 }
Sean Callanan8f2e3922012-02-04 08:49:35 +00001092
1093
1094 DeclFromParser <const RecordDecl> parser_record(record);
1095 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1096
1097 if (origin_record.IsInvalid())
1098 return false;
1099
1100 FieldOffsetMap origin_field_offsets;
1101 BaseOffsetMap origin_base_offsets;
1102 BaseOffsetMap origin_virtual_base_offsets;
1103
1104 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1105
1106 int field_idx = 0;
1107
1108 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1109 fi != fe;
1110 ++fi)
1111 {
1112 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1113
1114 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1115
1116 field_idx++;
1117 }
1118
1119 ASTContext &parser_ast_context(record->getASTContext());
1120
1121 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1122
1123 if (origin_cxx_record.IsValid())
1124 {
1125 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1126 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1127 return false;
1128 }
1129
1130 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1131 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1132 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1133 return false;
1134
1135 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1136 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1137
1138 if (log)
1139 {
1140 log->Printf("LRT[%u] returned:", current_id);
1141 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
1142 log->Printf("LRT[%u] Size = %lld", current_id, size);
1143 log->Printf("LRT[%u] Alignment = %lld", current_id, alignment);
1144 log->Printf("LRT[%u] Fields:", current_id);
1145 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1146 fi != fe;
1147 ++fi)
1148 {
1149 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %lld bits",
1150 current_id,
1151 *fi,
1152 fi->getNameAsString().c_str(),
1153 field_offsets[*fi]);
1154 }
1155 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1156 if (parser_cxx_record.IsValid())
1157 {
1158 log->Printf("LRT[%u] Bases:", current_id);
1159 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1160 bi != be;
1161 ++bi)
1162 {
1163 bool is_virtual = bi->isVirtual();
1164
1165 QualType base_type = bi->getType();
1166 const RecordType *base_record_type = base_type->getAs<RecordType>();
1167 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1168 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1169
1170 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %lld chars",
1171 current_id,
1172 (is_virtual ? "Virtual " : ""),
1173 base_cxx_record.decl,
1174 base_cxx_record.decl->getNameAsString().c_str(),
1175 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1176 base_offsets[base_cxx_record.decl].getQuantity()));
1177 }
1178 }
1179 else
1180 {
1181 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1182 }
1183 }
1184
1185 return true;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001186}
1187
Sean Callanan73b520f2011-10-29 01:58:46 +00001188void
1189ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1190 const ConstString &name,
1191 ClangASTImporter::NamespaceMapSP &parent_map) const
1192{
1193 static unsigned int invocation_id = 0;
1194 unsigned int current_id = invocation_id++;
1195
1196 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1197
1198 if (log)
1199 {
1200 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001201 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001202 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001203 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001204 name.GetCString(),
1205 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1206 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001207 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001208 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001209 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001210 name.GetCString());
1211 }
1212
1213
1214 if (parent_map)
1215 {
1216 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1217 i != e;
1218 ++i)
1219 {
1220 ClangNamespaceDecl found_namespace_decl;
1221
1222 lldb::ModuleSP module_sp = i->first;
1223 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1224
1225 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1226
1227 if (!symbol_vendor)
1228 continue;
1229
1230 SymbolContext null_sc;
1231
1232 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1233
1234 if (!found_namespace_decl)
1235 continue;
1236
1237 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1238
1239 if (log)
1240 log->Printf(" CMN[%u] Found namespace %s in module %s",
1241 current_id,
1242 name.GetCString(),
1243 module_sp->GetFileSpec().GetFilename().GetCString());
1244 }
1245 }
1246 else
1247 {
1248 ModuleList &images = m_target->GetImages();
1249 ClangNamespaceDecl null_namespace_decl;
1250
1251 for (uint32_t i = 0, e = images.GetSize();
1252 i != e;
1253 ++i)
1254 {
1255 lldb::ModuleSP image = images.GetModuleAtIndex(i);
1256
1257 if (!image)
1258 continue;
1259
1260 ClangNamespaceDecl found_namespace_decl;
1261
1262 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1263
1264 if (!symbol_vendor)
1265 continue;
1266
1267 SymbolContext null_sc;
1268
1269 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1270
1271 if (!found_namespace_decl)
1272 continue;
1273
1274 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1275
1276 if (log)
1277 log->Printf(" CMN[%u] Found namespace %s in module %s",
1278 current_id,
1279 name.GetCString(),
1280 image->GetFileSpec().GetFilename().GetCString());
1281 }
1282 }
1283}
1284
Sean Callananbb715f92011-10-29 02:28:18 +00001285NamespaceDecl *
1286ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1287{
Greg Clayton13d24fb2012-01-29 20:56:30 +00001288 if (!namespace_decls)
Sean Callananbb715f92011-10-29 02:28:18 +00001289 return NULL;
1290
1291 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1292
1293 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1294
Sean Callanan4938bd62011-11-16 18:20:47 +00001295 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +00001296
Sean Callananddb59332012-03-20 21:11:12 +00001297 if (!copied_decl)
1298 return NULL;
1299
Sean Callananbb715f92011-10-29 02:28:18 +00001300 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1301
1302 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1303
1304 return dyn_cast<NamespaceDecl>(copied_decl);
1305}
1306
Sean Callanan9394b5a2011-10-29 19:50:43 +00001307void *
1308ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1309 ASTContext *source_context,
1310 void *clang_type)
1311{
1312 SetImportInProgress(true);
1313
Sean Callanan4938bd62011-11-16 18:20:47 +00001314 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +00001315
1316 void *ret = ret_qual_type.getAsOpaquePtr();
1317
1318 SetImportInProgress(false);
1319
Sean Callananddb59332012-03-20 21:11:12 +00001320 if (ret && ret_qual_type->getCanonicalTypeInternal().isNull())
Sean Callananaa11af82012-02-27 19:22:39 +00001321 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1322 // on occasion.
1323 return NULL;
1324
Sean Callanan9394b5a2011-10-29 19:50:43 +00001325 return ret;
1326}
1327
Greg Claytonb01000f2011-01-17 03:46:26 +00001328clang::NamedDecl *
1329NameSearchContext::AddVarDecl(void *type)
1330{
Greg Clayton8de27c72010-10-15 22:48:33 +00001331 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +00001332
1333 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +00001334
Sean Callananf76afff2011-10-28 23:38:38 +00001335 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001336 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +00001337 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001338 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +00001339 ii,
Chris Lattner24943d22010-06-08 16:52:24 +00001340 QualType::getFromOpaquePtr(type),
1341 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001342 SC_Static,
1343 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +00001344 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +00001345
1346 return Decl;
1347}
Sean Callanan8f0dc342010-06-22 23:46:24 +00001348
Greg Claytonb01000f2011-01-17 03:46:26 +00001349clang::NamedDecl *
1350NameSearchContext::AddFunDecl (void *type)
1351{
Sean Callananf76afff2011-10-28 23:38:38 +00001352 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001353 const_cast<DeclContext*>(m_decl_context),
1354 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001355 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001356 m_decl_name.getAsIdentifierInfo(),
1357 QualType::getFromOpaquePtr(type),
1358 NULL,
1359 SC_Static,
1360 SC_Static,
1361 false,
1362 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001363
Sean Callananb291abe2010-08-12 23:45:38 +00001364 // We have to do more than just synthesize the FunctionDecl. We have to
1365 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1366 // this, we raid the function's FunctionProtoType for types.
1367
Greg Clayton8de27c72010-10-15 22:48:33 +00001368 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callanan21f2e192011-12-14 01:13:04 +00001369 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001370
Greg Clayton8de27c72010-10-15 22:48:33 +00001371 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +00001372 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001373 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001374 unsigned ArgIndex;
1375
Sean Callananc1535182011-10-07 23:18:13 +00001376 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1377
Sean Callanan8f0dc342010-06-22 23:46:24 +00001378 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1379 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001380 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001381
Sean Callananf76afff2011-10-28 23:38:38 +00001382 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +00001383 const_cast<DeclContext*>(m_decl_context),
1384 SourceLocation(),
1385 SourceLocation(),
1386 NULL,
1387 arg_qual_type,
1388 NULL,
1389 SC_Static,
1390 SC_Static,
1391 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001392 }
1393
Sean Callananc1535182011-10-07 23:18:13 +00001394 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001395 }
Sean Callanandc5fce12011-12-01 21:04:37 +00001396 else
1397 {
1398 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1399
1400 log->Printf("Function type wasn't a FunctionProtoType");
1401 }
Sean Callanan8f0dc342010-06-22 23:46:24 +00001402
Greg Clayton8de27c72010-10-15 22:48:33 +00001403 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001404
Greg Clayton8de27c72010-10-15 22:48:33 +00001405 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +00001406}
Sean Callanan0fc73582010-07-27 00:55:47 +00001407
Greg Claytonb01000f2011-01-17 03:46:26 +00001408clang::NamedDecl *
1409NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +00001410{
Sean Callananad293092011-01-18 23:32:05 +00001411 FunctionProtoType::ExtProtoInfo proto_info;
1412
1413 proto_info.Variadic = true;
1414
Sean Callananf76afff2011-10-28 23:38:38 +00001415 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1416 NULL, // argument types
1417 0, // number of arguments
1418 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +00001419
Sean Callanan0fc73582010-07-27 00:55:47 +00001420 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1421}
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001422
Greg Claytonb01000f2011-01-17 03:46:26 +00001423clang::NamedDecl *
1424NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001425{
Greg Claytona1aaaff2011-01-23 00:34:52 +00001426 if (type)
1427 {
1428 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001429
Sean Callanan21f2e192011-12-14 01:13:04 +00001430 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001431 {
1432 TagDecl *tag_decl = tag_type->getDecl();
1433
1434 m_decls.push_back(tag_decl);
1435
1436 return tag_decl;
1437 }
Sean Callanan21f2e192011-12-14 01:13:04 +00001438 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001439 {
1440 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1441
1442 m_decls.push_back((NamedDecl*)interface_decl);
1443
1444 return (NamedDecl*)interface_decl;
1445 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001446 }
Greg Claytona1aaaff2011-01-23 00:34:52 +00001447 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001448}
Greg Claytone6d72ca2011-06-25 00:44:06 +00001449
1450void
1451NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1452{
1453 for (clang::NamedDecl * const *decl_iterator = result.first;
1454 decl_iterator != result.second;
1455 ++decl_iterator)
1456 m_decls.push_back (*decl_iterator);
1457}
1458
1459void
1460NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1461{
1462 m_decls.push_back (decl);
1463}