blob: d488993b90d70cfbf096c8b39ee018fbb77d3373 [file] [log] [blame]
Greg Claytonea3b0ae2010-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 Lattner30fdc8d2010-06-08 16:52:24 +000010
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "clang/AST/ASTContext.h"
Sean Callanan5b26f272012-02-04 08:49:35 +000012#include "clang/AST/RecordLayout.h"
Greg Claytonee4b5dd2010-10-15 03:36:13 +000013#include "lldb/Core/Log.h"
Greg Clayton747bcb02011-09-17 06:21:20 +000014#include "lldb/Core/Module.h"
Sean Callanan1ee44b72011-10-29 01:58:46 +000015#include "lldb/Core/ModuleList.h"
Sean Callananba0aca72011-10-29 02:28:18 +000016#include "lldb/Expression/ASTDumper.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Expression/ClangASTSource.h"
18#include "lldb/Expression/ClangExpression.h"
Sean Callanan1ee44b72011-10-29 01:58:46 +000019#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Symbol/Function.h"
Sean Callanan1ee44b72011-10-29 01:58:46 +000021#include "lldb/Symbol/SymbolVendor.h"
Sean Callanan09ab4b72011-11-30 22:11:59 +000022#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan1ee44b72011-10-29 01:58:46 +000023#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
25using namespace clang;
26using namespace lldb_private;
27
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000028ClangASTSource::~ClangASTSource()
Greg Clayton6beaaa62011-01-17 03:46:26 +000029{
Sean Callanan99732312011-11-29 00:42:02 +000030 m_ast_importer->ForgetDestination(m_ast_context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000031
Johnny Chen60e2c6a2011-11-30 23:18:53 +000032 // We are in the process of destruction, don't create clang ast context on demand
33 // by passing false to Target::GetScratchClangASTContext(create_on_demand).
34 ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext(false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000035
Sean Callanan99732312011-11-29 00:42:02 +000036 if (!scratch_clang_ast_context)
37 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000038
Sean Callanan99732312011-11-29 00:42:02 +000039 clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000040
Sean Callanan99732312011-11-29 00:42:02 +000041 if (!scratch_ast_context)
42 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000043
Sean Callanan99732312011-11-29 00:42:02 +000044 if (m_ast_context != scratch_ast_context)
45 m_ast_importer->ForgetSource(scratch_ast_context, m_ast_context);
Greg Clayton6beaaa62011-01-17 03:46:26 +000046}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
Greg Clayton6beaaa62011-01-17 03:46:26 +000048void
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000049ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
Greg Clayton6beaaa62011-01-17 03:46:26 +000050{
Sean Callananeddeb3b2011-10-28 23:38:38 +000051 if (!m_ast_context)
52 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000053
Sean Callananeddeb3b2011-10-28 23:38:38 +000054 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
55 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056}
57
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058// The core lookup interface.
Sean Callananeeffea42013-02-12 08:01:13 +000059bool
Greg Clayton6beaaa62011-01-17 03:46:26 +000060ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonee4b5dd2010-10-15 03:36:13 +000061(
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000062 const DeclContext *decl_ctx,
Greg Clayton7b462cc2010-10-15 22:48:33 +000063 DeclarationName clang_decl_name
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000064)
Greg Claytonee4b5dd2010-10-15 03:36:13 +000065{
Sean Callananeddeb3b2011-10-28 23:38:38 +000066 if (!m_ast_context)
Sean Callananeeffea42013-02-12 08:01:13 +000067 {
68 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
69 return false;
70 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000071
Sean Callananeddeb3b2011-10-28 23:38:38 +000072 if (GetImportInProgress())
Sean Callananeeffea42013-02-12 08:01:13 +000073 {
74 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
75 return false;
76 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000077
Greg Clayton6beaaa62011-01-17 03:46:26 +000078 std::string decl_name (clang_decl_name.getAsString());
79
80// if (m_decl_map.DoingASTImport ())
81// return DeclContext::lookup_result();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000082//
Greg Clayton7b462cc2010-10-15 22:48:33 +000083 switch (clang_decl_name.getNameKind()) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 // Normal identifiers.
85 case DeclarationName::Identifier:
Sean Callanane8dea982012-04-25 17:46:01 +000086 {
87 clang::IdentifierInfo *identifier_info = clang_decl_name.getAsIdentifierInfo();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000088
Sean Callanane8dea982012-04-25 17:46:01 +000089 if (!identifier_info ||
90 identifier_info->getBuiltinID() != 0)
91 {
Sean Callananeeffea42013-02-12 08:01:13 +000092 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
93 return false;
Sean Callanane8dea982012-04-25 17:46:01 +000094 }
95 }
Greg Clayton7b462cc2010-10-15 22:48:33 +000096 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000097
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 // Operator names. Not important for now.
99 case DeclarationName::CXXOperatorName:
100 case DeclarationName::CXXLiteralOperatorName:
Sean Callananeeffea42013-02-12 08:01:13 +0000101 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
102 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000103
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 // Using directives found in this context.
105 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
106 case DeclarationName::CXXUsingDirective:
Sean Callananeeffea42013-02-12 08:01:13 +0000107 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
108 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000109
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 case DeclarationName::ObjCZeroArgSelector:
111 case DeclarationName::ObjCOneArgSelector:
112 case DeclarationName::ObjCMultiArgSelector:
Sean Callanan0730e9c2011-11-09 19:33:21 +0000113 {
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000114 llvm::SmallVector<NamedDecl*, 1> method_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115
Sean Callanan0730e9c2011-11-09 19:33:21 +0000116 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000117
Sean Callanan0730e9c2011-11-09 19:33:21 +0000118 FindObjCMethodDecls(method_search_context);
119
Sean Callananeeffea42013-02-12 08:01:13 +0000120 SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
121 return (method_decls.size() > 0);
Sean Callanan0730e9c2011-11-09 19:33:21 +0000122 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 // These aren't possible in the global context.
124 case DeclarationName::CXXConstructorName:
125 case DeclarationName::CXXDestructorName:
126 case DeclarationName::CXXConversionFunctionName:
Sean Callananeeffea42013-02-12 08:01:13 +0000127 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
128 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 }
Greg Claytonee4b5dd2010-10-15 03:36:13 +0000130
Greg Claytonee4b5dd2010-10-15 03:36:13 +0000131
Sean Callananeddeb3b2011-10-28 23:38:38 +0000132 if (!GetLookupsEnabled())
Greg Clayton7b462cc2010-10-15 22:48:33 +0000133 {
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000134 // Wait until we see a '$' at the start of a name before we start doing
Greg Clayton7b462cc2010-10-15 22:48:33 +0000135 // any lookups so we can avoid lookup up all of the builtin types.
136 if (!decl_name.empty() && decl_name[0] == '$')
137 {
Sean Callananeddeb3b2011-10-28 23:38:38 +0000138 SetLookupsEnabled (true);
Greg Clayton7b462cc2010-10-15 22:48:33 +0000139 }
140 else
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000141 {
Sean Callananeeffea42013-02-12 08:01:13 +0000142 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
143 return false;
Greg Clayton7b462cc2010-10-15 22:48:33 +0000144 }
Greg Claytonee4b5dd2010-10-15 03:36:13 +0000145 }
Greg Clayton7b462cc2010-10-15 22:48:33 +0000146
Greg Clayton7b462cc2010-10-15 22:48:33 +0000147 ConstString const_decl_name(decl_name.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000148
Greg Clayton580c5da2010-11-13 04:18:24 +0000149 const char *uniqued_const_decl_name = const_decl_name.GetCString();
150 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
151 {
152 // We are currently looking up this name...
Sean Callananeeffea42013-02-12 08:01:13 +0000153 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
154 return false;
Greg Clayton580c5da2010-11-13 04:18:24 +0000155 }
156 m_active_lookups.insert(uniqued_const_decl_name);
Greg Clayton471da242010-11-15 01:34:18 +0000157// static uint32_t g_depth = 0;
158// ++g_depth;
159// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000160 llvm::SmallVector<NamedDecl*, 4> name_decls;
Greg Clayton580c5da2010-11-13 04:18:24 +0000161 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananeddeb3b2011-10-28 23:38:38 +0000162 FindExternalVisibleDecls(name_search_context);
Sean Callananeeffea42013-02-12 08:01:13 +0000163 SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls);
Greg Clayton471da242010-11-15 01:34:18 +0000164// --g_depth;
Greg Clayton580c5da2010-11-13 04:18:24 +0000165 m_active_lookups.erase (uniqued_const_decl_name);
Sean Callananeeffea42013-02-12 08:01:13 +0000166 return (name_decls.size() != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
Greg Clayton6beaaa62011-01-17 03:46:26 +0000169void
170ClangASTSource::CompleteType (TagDecl *tag_decl)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000171{
Greg Clayton5160ce52013-03-27 23:08:40 +0000172 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000173
Sean Callanan12014a02011-12-08 23:45:45 +0000174 static unsigned int invocation_id = 0;
175 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000176
Sean Callananba0aca72011-10-29 02:28:18 +0000177 if (log)
178 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000179 log->Printf(" CompleteTagDecl[%u] on (ASTContext*)%p Completing (TagDecl*)%p named %s",
180 current_id, static_cast<void*>(m_ast_context),
181 static_cast<void*>(tag_decl),
Sean Callanan12014a02011-12-08 23:45:45 +0000182 tag_decl->getName().str().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000183
Sean Callanan12014a02011-12-08 23:45:45 +0000184 log->Printf(" CTD[%u] Before:", current_id);
Sean Callananba0aca72011-10-29 02:28:18 +0000185 ASTDumper dumper((Decl*)tag_decl);
186 dumper.ToLog(log, " [CTD] ");
187 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000188
Sean Callanan12014a02011-12-08 23:45:45 +0000189 if (!m_ast_importer->CompleteTagDecl (tag_decl))
190 {
191 // We couldn't complete the type. Maybe there's a definition
192 // somewhere else that can be completed.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000193
Sean Callanan12014a02011-12-08 23:45:45 +0000194 if (log)
195 log->Printf(" CTD[%u] Type could not be completed in the module in which it was first found.", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000196
Sean Callanan12014a02011-12-08 23:45:45 +0000197 bool found = false;
198
199 DeclContext *decl_ctx = tag_decl->getDeclContext();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000200
Sean Callanan12014a02011-12-08 23:45:45 +0000201 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(decl_ctx))
202 {
203 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000204
Sean Callanan12014a02011-12-08 23:45:45 +0000205 if (log && log->GetVerbose())
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000206 log->Printf(" CTD[%u] Inspecting namespace map %p (%d entries)",
207 current_id, static_cast<void*>(namespace_map.get()),
208 static_cast<int>(namespace_map->size()));
209
Sean Callanan12014a02011-12-08 23:45:45 +0000210 if (!namespace_map)
211 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000212
Sean Callanan12014a02011-12-08 23:45:45 +0000213 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
214 i != e && !found;
215 ++i)
216 {
217 if (log)
218 log->Printf(" CTD[%u] Searching namespace %s in module %s",
219 current_id,
220 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
221 i->first->GetFileSpec().GetFilename().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000222
Sean Callanan12014a02011-12-08 23:45:45 +0000223 TypeList types;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000224
Sean Callanan12014a02011-12-08 23:45:45 +0000225 SymbolContext null_sc;
226 ConstString name(tag_decl->getName().str().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000227
Greg Clayton84db9102012-03-26 23:03:23 +0000228 i->first->FindTypesInNamespace(null_sc, name, &i->second, UINT32_MAX, types);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000229
Sean Callanan12014a02011-12-08 23:45:45 +0000230 for (uint32_t ti = 0, te = types.GetSize();
231 ti != te && !found;
232 ++ti)
233 {
234 lldb::TypeSP type = types.GetTypeAtIndex(ti);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000235
Sean Callanan12014a02011-12-08 23:45:45 +0000236 if (!type)
237 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000238
Greg Clayton57ee3062013-07-11 22:46:58 +0000239 ClangASTType clang_type (type->GetClangFullType());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000240
Greg Clayton57ee3062013-07-11 22:46:58 +0000241 if (!clang_type)
Sean Callanan12014a02011-12-08 23:45:45 +0000242 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000243
Greg Clayton57ee3062013-07-11 22:46:58 +0000244 const TagType *tag_type = clang_type.GetQualType()->getAs<TagType>();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000245
Sean Callanan12014a02011-12-08 23:45:45 +0000246 if (!tag_type)
247 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000248
Sean Callanan12014a02011-12-08 23:45:45 +0000249 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000250
Sean Callanan12014a02011-12-08 23:45:45 +0000251 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
252 found = true;
253 }
254 }
255 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000256 else
Sean Callanan12014a02011-12-08 23:45:45 +0000257 {
258 TypeList types;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000259
Sean Callanan12014a02011-12-08 23:45:45 +0000260 SymbolContext null_sc;
261 ConstString name(tag_decl->getName().str().c_str());
262 ClangNamespaceDecl namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000263
Enrico Granata17598482012-11-08 02:22:02 +0000264 const ModuleList &module_list = m_target->GetImages();
Sean Callanan12014a02011-12-08 23:45:45 +0000265
Greg Clayton84db9102012-03-26 23:03:23 +0000266 bool exact_match = false;
Greg Clayton29399a22012-04-06 17:41:13 +0000267 module_list.FindTypes (null_sc, name, exact_match, UINT32_MAX, types);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000268
Sean Callanan12014a02011-12-08 23:45:45 +0000269 for (uint32_t ti = 0, te = types.GetSize();
270 ti != te && !found;
271 ++ti)
272 {
273 lldb::TypeSP type = types.GetTypeAtIndex(ti);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000274
Sean Callanan12014a02011-12-08 23:45:45 +0000275 if (!type)
276 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000277
Greg Clayton57ee3062013-07-11 22:46:58 +0000278 ClangASTType clang_type (type->GetClangFullType());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000279
Greg Clayton57ee3062013-07-11 22:46:58 +0000280 if (!clang_type)
Sean Callanan12014a02011-12-08 23:45:45 +0000281 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000282
Greg Clayton57ee3062013-07-11 22:46:58 +0000283 const TagType *tag_type = clang_type.GetQualType()->getAs<TagType>();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000284
Sean Callanan12014a02011-12-08 23:45:45 +0000285 if (!tag_type)
286 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000287
Sean Callanan12014a02011-12-08 23:45:45 +0000288 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000289
Sean Callanan12014a02011-12-08 23:45:45 +0000290 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
291 found = true;
292 }
293 }
294 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000295
Sean Callananba0aca72011-10-29 02:28:18 +0000296 if (log)
297 {
298 log->Printf(" [CTD] After:");
299 ASTDumper dumper((Decl*)tag_decl);
300 dumper.ToLog(log, " [CTD] ");
301 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000302}
303
304void
Sean Callananba0aca72011-10-29 02:28:18 +0000305ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000306{
Greg Clayton5160ce52013-03-27 23:08:40 +0000307 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000308
Sean Callananba0aca72011-10-29 02:28:18 +0000309 if (log)
310 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000311 log->Printf(" [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing an ObjCInterfaceDecl named %s",
312 static_cast<void*>(m_ast_context),
313 interface_decl->getName().str().c_str());
Sean Callananba0aca72011-10-29 02:28:18 +0000314 log->Printf(" [COID] Before:");
315 ASTDumper dumper((Decl*)interface_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000316 dumper.ToLog(log, " [COID] ");
Sean Callananba0aca72011-10-29 02:28:18 +0000317 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000318
Sean Callanane3236a12014-03-11 19:34:04 +0000319 Decl *original_decl = NULL;
320 ASTContext *original_ctx = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000321
Sean Callanane3236a12014-03-11 19:34:04 +0000322 if (m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx))
323 {
324 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
325 {
326 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000327
Sean Callanane3236a12014-03-11 19:34:04 +0000328 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
329 {
330 m_ast_importer->SetDeclOrigin(interface_decl, original_iface_decl);
331 }
332 }
333 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000334
Sean Callananba0aca72011-10-29 02:28:18 +0000335 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000336
Sean Callananf4c0a222013-12-20 04:09:05 +0000337 if (interface_decl->getSuperClass() &&
338 interface_decl->getSuperClass() != interface_decl)
339 CompleteType(interface_decl->getSuperClass());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000340
Sean Callananba0aca72011-10-29 02:28:18 +0000341 if (log)
342 {
343 log->Printf(" [COID] After:");
344 ASTDumper dumper((Decl*)interface_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000345 dumper.ToLog(log, " [COID] ");
Sean Callananba0aca72011-10-29 02:28:18 +0000346 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000347}
348
Sean Callanan2cb5e522012-09-20 23:21:16 +0000349clang::ObjCInterfaceDecl *
350ClangASTSource::GetCompleteObjCInterface (clang::ObjCInterfaceDecl *interface_decl)
351{
352 lldb::ProcessSP process(m_target->GetProcessSP());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000353
Sean Callanan2cb5e522012-09-20 23:21:16 +0000354 if (!process)
355 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000356
Sean Callanan2cb5e522012-09-20 23:21:16 +0000357 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000358
Sean Callanan2cb5e522012-09-20 23:21:16 +0000359 if (!language_runtime)
360 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000361
Sean Callanan2cb5e522012-09-20 23:21:16 +0000362 ConstString class_name(interface_decl->getNameAsString().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000363
Sean Callanan2cb5e522012-09-20 23:21:16 +0000364 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000365
Sean Callanan2cb5e522012-09-20 23:21:16 +0000366 if (!complete_type_sp)
367 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000368
Greg Clayton57ee3062013-07-11 22:46:58 +0000369 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType());
Sean Callanan2cb5e522012-09-20 23:21:16 +0000370 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000371
Sean Callanan2cb5e522012-09-20 23:21:16 +0000372 if (!complete_opaque_type)
373 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000374
Sean Callanan2cb5e522012-09-20 23:21:16 +0000375 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
376 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000377
Sean Callanan2cb5e522012-09-20 23:21:16 +0000378 if (!complete_interface_type)
379 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000380
Sean Callanan2cb5e522012-09-20 23:21:16 +0000381 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000382
Sean Callanan2cb5e522012-09-20 23:21:16 +0000383 return complete_iface_decl;
384}
385
Sean Callanancc427fa2011-07-30 02:42:06 +0000386clang::ExternalLoadResult
Sean Callanan2cb5e522012-09-20 23:21:16 +0000387ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000388 bool (*predicate)(Decl::Kind),
389 llvm::SmallVectorImpl<Decl*> &decls)
Sean Callanan8106d802013-03-08 20:04:57 +0000390{
391 ClangASTMetrics::RegisterLexicalQuery();
392
Greg Clayton5160ce52013-03-27 23:08:40 +0000393 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000394
Sean Callananba0aca72011-10-29 02:28:18 +0000395 const Decl *context_decl = dyn_cast<Decl>(decl_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000396
Sean Callananba0aca72011-10-29 02:28:18 +0000397 if (!context_decl)
398 return ELR_Failure;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000399
Sean Callananba0aca72011-10-29 02:28:18 +0000400 static unsigned int invocation_id = 0;
401 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000402
Sean Callananba0aca72011-10-29 02:28:18 +0000403 if (log)
404 {
405 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan00f43622011-11-18 03:28:09 +0000406 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000407 current_id, static_cast<void*>(m_ast_context),
Sean Callananba0aca72011-10-29 02:28:18 +0000408 context_named_decl->getNameAsString().c_str(),
Sean Callanan00f43622011-11-18 03:28:09 +0000409 context_decl->getDeclKindName(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000410 static_cast<const void*>(context_decl),
Sean Callananba0aca72011-10-29 02:28:18 +0000411 (predicate ? "non-null" : "null"));
412 else if(context_decl)
Sean Callanan00f43622011-11-18 03:28:09 +0000413 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000414 current_id, static_cast<void*>(m_ast_context),
415 context_decl->getDeclKindName(),
416 static_cast<const void*>(context_decl),
Sean Callananba0aca72011-10-29 02:28:18 +0000417 (predicate ? "non-null" : "null"));
418 else
Sean Callanan00f43622011-11-18 03:28:09 +0000419 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000420 current_id, static_cast<const void*>(m_ast_context),
Sean Callananba0aca72011-10-29 02:28:18 +0000421 (predicate ? "non-null" : "null"));
422 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000423
Sean Callananba0aca72011-10-29 02:28:18 +0000424 Decl *original_decl = NULL;
425 ASTContext *original_ctx = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000426
Sean Callananba0aca72011-10-29 02:28:18 +0000427 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
428 return ELR_Failure;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000429
Sean Callananba0aca72011-10-29 02:28:18 +0000430 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000431 {
432 log->Printf(" FELD[%u] Original decl (ASTContext*)%p (Decl*)%p:",
433 current_id, static_cast<void*>(original_ctx),
434 static_cast<void*>(original_decl));
Sean Callananba0aca72011-10-29 02:28:18 +0000435 ASTDumper(original_decl).ToLog(log, " ");
436 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000437
Sean Callanan2cb5e522012-09-20 23:21:16 +0000438 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
439 {
440 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000441
Sean Callanan2cb5e522012-09-20 23:21:16 +0000442 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
443 {
444 original_decl = complete_iface_decl;
445 original_ctx = &complete_iface_decl->getASTContext();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000446
Sean Callanan2cb5e522012-09-20 23:21:16 +0000447 m_ast_importer->SetDeclOrigin(context_decl, original_iface_decl);
448 }
449 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000450
Sean Callananba0aca72011-10-29 02:28:18 +0000451 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
452 {
453 ExternalASTSource *external_source = original_ctx->getExternalSource();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000454
Sean Callananba0aca72011-10-29 02:28:18 +0000455 if (external_source)
456 external_source->CompleteType (original_tag_decl);
457 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000458
Sean Callanan12014a02011-12-08 23:45:45 +0000459 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000460
Sean Callananba0aca72011-10-29 02:28:18 +0000461 if (!original_decl_context)
462 return ELR_Failure;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000463
Sean Callananba0aca72011-10-29 02:28:18 +0000464 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
465 iter != original_decl_context->decls_end();
466 ++iter)
467 {
468 Decl *decl = *iter;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000469
Sean Callananba0aca72011-10-29 02:28:18 +0000470 if (!predicate || predicate(decl->getKind()))
471 {
472 if (log)
473 {
474 ASTDumper ast_dumper(decl);
475 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan3d654b32012-09-24 22:25:51 +0000476 log->Printf(" FELD[%d] Adding [to %sDecl %s] lexical %sDecl %s", current_id, context_named_decl->getDeclKindName(), context_named_decl->getNameAsString().c_str(), decl->getDeclKindName(), ast_dumper.GetCString());
Sean Callananba0aca72011-10-29 02:28:18 +0000477 else
Sean Callanan3d654b32012-09-24 22:25:51 +0000478 log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString());
Sean Callananba0aca72011-10-29 02:28:18 +0000479 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000480
Sean Callanan686b2312011-11-16 18:20:47 +0000481 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000482
Sean Callanan61b33f72012-03-20 21:11:12 +0000483 if (!copied_decl)
484 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000485
Sean Callanancf128622012-03-15 01:53:17 +0000486 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
487 {
488 QualType copied_field_type = copied_field->getType();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000489
Sean Callanan6b200d02013-03-21 22:15:41 +0000490 m_ast_importer->RequireCompleteType(copied_field_type);
Sean Callanancf128622012-03-15 01:53:17 +0000491 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000492
Sean Callananba0aca72011-10-29 02:28:18 +0000493 decls.push_back(copied_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000494
Sean Callanan04b2bfa2013-05-09 01:09:49 +0000495 DeclContext *decl_context_non_const = const_cast<DeclContext *>(decl_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000496
Sean Callanan04b2bfa2013-05-09 01:09:49 +0000497 if (copied_decl->getDeclContext() != decl_context)
498 {
499 if (copied_decl->getDeclContext()->containsDecl(copied_decl))
500 copied_decl->getDeclContext()->removeDecl(copied_decl);
501 copied_decl->setDeclContext(decl_context_non_const);
502 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000503
Sean Callanan04b2bfa2013-05-09 01:09:49 +0000504 if (!decl_context_non_const->containsDecl(copied_decl))
505 decl_context_non_const->addDeclInternal(copied_decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000506 }
507 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000508
Sean Callananba0aca72011-10-29 02:28:18 +0000509 return ELR_AlreadyLoaded;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510}
511
Sean Callananfb3e4302011-10-29 19:50:43 +0000512void
513ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
514{
515 assert (m_ast_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000516
Sean Callanan8106d802013-03-08 20:04:57 +0000517 ClangASTMetrics::RegisterVisibleQuery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000518
Sean Callananfb3e4302011-10-29 19:50:43 +0000519 const ConstString name(context.m_decl_name.getAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000520
Greg Clayton5160ce52013-03-27 23:08:40 +0000521 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000522
Sean Callananfb3e4302011-10-29 19:50:43 +0000523 static unsigned int invocation_id = 0;
524 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000525
Sean Callananfb3e4302011-10-29 19:50:43 +0000526 if (log)
527 {
528 if (!context.m_decl_context)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000529 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a NULL DeclContext",
530 current_id, static_cast<void*>(m_ast_context),
531 name.GetCString());
Sean Callananfb3e4302011-10-29 19:50:43 +0000532 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000533 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in '%s'",
534 current_id, static_cast<void*>(m_ast_context),
535 name.GetCString(),
536 context_named_decl->getNameAsString().c_str());
Sean Callananfb3e4302011-10-29 19:50:43 +0000537 else
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000538 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a '%s'",
539 current_id, static_cast<void*>(m_ast_context),
540 name.GetCString(),
541 context.m_decl_context->getDeclKindName());
Sean Callananfb3e4302011-10-29 19:50:43 +0000542 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000543
Sean Callananfb3e4302011-10-29 19:50:43 +0000544 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000545
Sean Callananfb3e4302011-10-29 19:50:43 +0000546 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
547 {
548 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000549
Sean Callananfb3e4302011-10-29 19:50:43 +0000550 if (log && log->GetVerbose())
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000551 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
552 current_id, static_cast<void*>(namespace_map.get()),
553 static_cast<int>(namespace_map->size()));
554
Sean Callananfb3e4302011-10-29 19:50:43 +0000555 if (!namespace_map)
556 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000557
Sean Callananfb3e4302011-10-29 19:50:43 +0000558 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
559 i != e;
560 ++i)
561 {
562 if (log)
563 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
564 current_id,
565 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
566 i->first->GetFileSpec().GetFilename().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000567
Sean Callananfb3e4302011-10-29 19:50:43 +0000568 FindExternalVisibleDecls(context,
569 i->first,
570 i->second,
571 current_id);
572 }
573 }
Sean Callanan100d74e2011-11-15 21:50:18 +0000574 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanand5c17ed2011-11-15 02:11:17 +0000575 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000576 FindObjCPropertyAndIvarDecls(context);
Sean Callanand5c17ed2011-11-15 02:11:17 +0000577 }
Sean Callananfb3e4302011-10-29 19:50:43 +0000578 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
579 {
580 // we shouldn't be getting FindExternalVisibleDecls calls for these
581 return;
582 }
583 else
584 {
585 ClangNamespaceDecl namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000586
Sean Callananfb3e4302011-10-29 19:50:43 +0000587 if (log)
588 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000589
Sean Callananfb3e4302011-10-29 19:50:43 +0000590 FindExternalVisibleDecls(context,
591 lldb::ModuleSP(),
592 namespace_decl,
593 current_id);
594 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000595
Sean Callananfb3e4302011-10-29 19:50:43 +0000596 if (!context.m_namespace_map->empty())
597 {
598 if (log && log->GetVerbose())
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000599 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
Sean Callananfb3e4302011-10-29 19:50:43 +0000600 current_id,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000601 static_cast<void*>(context.m_namespace_map.get()),
602 static_cast<int>(context.m_namespace_map->size()));
603
Sean Callananfb3e4302011-10-29 19:50:43 +0000604 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000605
Sean Callananfb3e4302011-10-29 19:50:43 +0000606 if (clang_namespace_decl)
607 clang_namespace_decl->setHasExternalVisibleStorage();
608 }
609}
610
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000611void
612ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
Sean Callananfb3e4302011-10-29 19:50:43 +0000613 lldb::ModuleSP module_sp,
614 ClangNamespaceDecl &namespace_decl,
615 unsigned int current_id)
616{
617 assert (m_ast_context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000618
Greg Clayton5160ce52013-03-27 23:08:40 +0000619 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000620
Sean Callananfb3e4302011-10-29 19:50:43 +0000621 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000622
Sean Callananfb3e4302011-10-29 19:50:43 +0000623 const ConstString name(context.m_decl_name.getAsString().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000624
Sean Callananfb3e4302011-10-29 19:50:43 +0000625 const char *name_unique_cstr = name.GetCString();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000626
Sean Callanan5b26f272012-02-04 08:49:35 +0000627 static ConstString id_name("id");
628 static ConstString Class_name("Class");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000629
Sean Callanan5b26f272012-02-04 08:49:35 +0000630 if (name == id_name || name == Class_name)
631 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000632
Sean Callananfb3e4302011-10-29 19:50:43 +0000633 if (name_unique_cstr == NULL)
634 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000635
Sean Callananfb3e4302011-10-29 19:50:43 +0000636 // The ClangASTSource is not responsible for finding $-names.
637 if (name_unique_cstr[0] == '$')
638 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000639
Sean Callananfb3e4302011-10-29 19:50:43 +0000640 if (module_sp && namespace_decl)
641 {
642 ClangNamespaceDecl found_namespace_decl;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000643
Sean Callananfb3e4302011-10-29 19:50:43 +0000644 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000645
Sean Callananfb3e4302011-10-29 19:50:43 +0000646 if (symbol_vendor)
647 {
648 SymbolContext null_sc;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000649
Sean Callananfb3e4302011-10-29 19:50:43 +0000650 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000651
Sean Callananfb3e4302011-10-29 19:50:43 +0000652 if (found_namespace_decl)
653 {
654 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000655
Sean Callananfb3e4302011-10-29 19:50:43 +0000656 if (log)
657 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
658 current_id,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000659 name.GetCString(),
Sean Callananfb3e4302011-10-29 19:50:43 +0000660 module_sp->GetFileSpec().GetFilename().GetCString());
661 }
662 }
663 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000664 else
Sean Callananfb3e4302011-10-29 19:50:43 +0000665 {
Enrico Granata17598482012-11-08 02:22:02 +0000666 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000667 Mutex::Locker modules_locker (target_images.GetMutex());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000668
Greg Claytonc7bece562013-01-25 18:06:21 +0000669 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callananfb3e4302011-10-29 19:50:43 +0000670 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000671 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000672
Sean Callananfb3e4302011-10-29 19:50:43 +0000673 if (!image)
674 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000675
Sean Callananfb3e4302011-10-29 19:50:43 +0000676 ClangNamespaceDecl found_namespace_decl;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000677
Sean Callananfb3e4302011-10-29 19:50:43 +0000678 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000679
Sean Callananfb3e4302011-10-29 19:50:43 +0000680 if (!symbol_vendor)
681 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000682
Sean Callananfb3e4302011-10-29 19:50:43 +0000683 SymbolContext null_sc;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000684
Sean Callananfb3e4302011-10-29 19:50:43 +0000685 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000686
Sean Callananfb3e4302011-10-29 19:50:43 +0000687 if (found_namespace_decl)
688 {
689 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000690
Sean Callananfb3e4302011-10-29 19:50:43 +0000691 if (log)
692 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
693 current_id,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000694 name.GetCString(),
Sean Callananfb3e4302011-10-29 19:50:43 +0000695 image->GetFileSpec().GetFilename().GetCString());
696 }
697 }
698 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000699
700 do
Sean Callananfb3e4302011-10-29 19:50:43 +0000701 {
702 TypeList types;
703 SymbolContext null_sc;
Greg Clayton84db9102012-03-26 23:03:23 +0000704 const bool exact_match = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000705
Sean Callananfb3e4302011-10-29 19:50:43 +0000706 if (module_sp && namespace_decl)
Greg Clayton84db9102012-03-26 23:03:23 +0000707 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000708 else
Greg Clayton29399a22012-04-06 17:41:13 +0000709 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000710
Sean Callananfb3e4302011-10-29 19:50:43 +0000711 if (types.GetSize())
712 {
713 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000714
Sean Callananfb3e4302011-10-29 19:50:43 +0000715 if (log)
716 {
717 const char *name_string = type_sp->GetName().GetCString();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000718
719 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
720 current_id,
721 name.GetCString(),
Sean Callananfb3e4302011-10-29 19:50:43 +0000722 (name_string ? name_string : "<anonymous>"));
723 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000724
Greg Clayton57ee3062013-07-11 22:46:58 +0000725 ClangASTType full_type = type_sp->GetClangFullType();
Sean Callanan0eed0d42011-12-06 03:41:14 +0000726
Greg Clayton57ee3062013-07-11 22:46:58 +0000727 ClangASTType copied_clang_type (GuardedCopyType(full_type));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000728
Greg Clayton57ee3062013-07-11 22:46:58 +0000729 if (!copied_clang_type)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000730 {
Sean Callanane0a64f72011-12-01 21:04:37 +0000731 if (log)
Sean Callanan7be70e82012-12-19 23:05:01 +0000732 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
Sean Callanan0eed0d42011-12-06 03:41:14 +0000733 current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000734
Sean Callanane0a64f72011-12-01 21:04:37 +0000735 break;
736 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000737
Greg Clayton57ee3062013-07-11 22:46:58 +0000738 context.AddTypeDecl(copied_clang_type);
Sean Callananfb3e4302011-10-29 19:50:43 +0000739 }
Sean Callanan7be70e82012-12-19 23:05:01 +0000740 else
741 {
742 do
743 {
744 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000745
Sean Callanan7be70e82012-12-19 23:05:01 +0000746 lldb::ProcessSP process(m_target->GetProcessSP());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000747
Sean Callanan7be70e82012-12-19 23:05:01 +0000748 if (!process)
749 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000750
Sean Callanan7be70e82012-12-19 23:05:01 +0000751 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000752
Sean Callanan7be70e82012-12-19 23:05:01 +0000753 if (!language_runtime)
754 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000755
Sean Callanan7be70e82012-12-19 23:05:01 +0000756 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000757
Sean Callanan7be70e82012-12-19 23:05:01 +0000758 if (!type_vendor)
759 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000760
Sean Callanan7be70e82012-12-19 23:05:01 +0000761 bool append = false;
762 uint32_t max_matches = 1;
763 std::vector <ClangASTType> types;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000764
Sean Callanan7be70e82012-12-19 23:05:01 +0000765 if (!type_vendor->FindTypes(name,
766 append,
767 max_matches,
768 types))
769 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000770
Sean Callanan7be70e82012-12-19 23:05:01 +0000771 if (log)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000772 {
Sean Callanan7be70e82012-12-19 23:05:01 +0000773 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
774 current_id,
775 name.GetCString());
776 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000777
Greg Clayton57ee3062013-07-11 22:46:58 +0000778 ClangASTType copied_clang_type (GuardedCopyType(types[0]));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000779
Greg Clayton57ee3062013-07-11 22:46:58 +0000780 if (!copied_clang_type)
Sean Callanan7be70e82012-12-19 23:05:01 +0000781 {
782 if (log)
783 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
784 current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000785
Sean Callanan7be70e82012-12-19 23:05:01 +0000786 break;
787 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000788
Greg Clayton57ee3062013-07-11 22:46:58 +0000789 context.AddTypeDecl(copied_clang_type);
Sean Callanan7be70e82012-12-19 23:05:01 +0000790 }
791 while(0);
792 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000793
Sean Callananfb3e4302011-10-29 19:50:43 +0000794 } while(0);
795}
796
Sean Callanan55400942012-11-02 17:09:58 +0000797template <class D> class TaggedASTDecl {
798public:
799 TaggedASTDecl() : decl(NULL) { }
800 TaggedASTDecl(D *_decl) : decl(_decl) { }
801 bool IsValid() const { return (decl != NULL); }
802 bool IsInvalid() const { return !IsValid(); }
803 D *operator->() const { return decl; }
804 D *decl;
805};
806
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000807template <class D2, template <class D> class TD, class D1>
Sean Callanan55400942012-11-02 17:09:58 +0000808TD<D2>
809DynCast(TD<D1> source)
810{
811 return TD<D2> (dyn_cast<D2>(source.decl));
812}
813
814template <class D = Decl> class DeclFromParser;
815template <class D = Decl> class DeclFromUser;
816
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000817template <class D> class DeclFromParser : public TaggedASTDecl<D> {
Sean Callanan55400942012-11-02 17:09:58 +0000818public:
819 DeclFromParser() : TaggedASTDecl<D>() { }
820 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000821
Sean Callanan55400942012-11-02 17:09:58 +0000822 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
823};
824
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000825template <class D> class DeclFromUser : public TaggedASTDecl<D> {
Sean Callanan55400942012-11-02 17:09:58 +0000826public:
827 DeclFromUser() : TaggedASTDecl<D>() { }
828 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000829
Sean Callanan55400942012-11-02 17:09:58 +0000830 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
831};
832
833template <class D>
834DeclFromUser<D>
835DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
836{
837 DeclFromUser <> origin_decl;
838 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
839 if (origin_decl.IsInvalid())
840 return DeclFromUser<D>();
841 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
842}
843
844template <class D>
845DeclFromParser<D>
846DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
847{
848 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
849 if (parser_generic_decl.IsInvalid())
850 return DeclFromParser<D>();
851 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
852}
853
Sean Callananc83e3412012-11-28 03:23:20 +0000854static bool
Sean Callananbc47dfc2012-09-11 21:44:01 +0000855FindObjCMethodDeclsWithOrigin (unsigned int current_id,
856 NameSearchContext &context,
857 ObjCInterfaceDecl *original_interface_decl,
858 clang::ASTContext *ast_context,
859 ClangASTImporter *ast_importer,
860 const char *log_info)
861{
862 const DeclarationName &decl_name(context.m_decl_name);
863 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
864
865 Selector original_selector;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000866
Sean Callananbc47dfc2012-09-11 21:44:01 +0000867 if (decl_name.isObjCZeroArgSelector())
868 {
869 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
870 original_selector = original_ctx->Selectors.getSelector(0, &ident);
871 }
872 else if (decl_name.isObjCOneArgSelector())
873 {
874 const std::string &decl_name_string = decl_name.getAsString();
875 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
876 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
877 original_selector = original_ctx->Selectors.getSelector(1, &ident);
878 }
879 else
880 {
881 SmallVector<IdentifierInfo *, 4> idents;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000882
Sean Callananbc47dfc2012-09-11 21:44:01 +0000883 clang::Selector sel = decl_name.getObjCSelector();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000884
Andy Gibbsa297a972013-06-19 19:04:53 +0000885 unsigned num_args = sel.getNumArgs();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000886
Sean Callananbc47dfc2012-09-11 21:44:01 +0000887 for (unsigned i = 0;
888 i != num_args;
889 ++i)
890 {
891 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
892 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000893
Sean Callananbc47dfc2012-09-11 21:44:01 +0000894 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
895 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000896
Sean Callananbc47dfc2012-09-11 21:44:01 +0000897 DeclarationName original_decl_name(original_selector);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000898
Sean Callananbc47dfc2012-09-11 21:44:01 +0000899 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000900
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000901 if (result.empty())
Sean Callananc83e3412012-11-28 03:23:20 +0000902 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000903
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000904 if (!result[0])
Sean Callananc83e3412012-11-28 03:23:20 +0000905 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000906
Sean Callanan01c8cb82013-09-04 23:25:26 +0000907 for (NamedDecl *named_decl : result)
Sean Callananbc47dfc2012-09-11 21:44:01 +0000908 {
Sean Callanan01c8cb82013-09-04 23:25:26 +0000909 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000910
Sean Callanan01c8cb82013-09-04 23:25:26 +0000911 if (!result_method)
912 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000913
Sean Callanan01c8cb82013-09-04 23:25:26 +0000914 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000915
Sean Callanan01c8cb82013-09-04 23:25:26 +0000916 if (!copied_decl)
917 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000918
Sean Callanan01c8cb82013-09-04 23:25:26 +0000919 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000920
Sean Callanan01c8cb82013-09-04 23:25:26 +0000921 if (!copied_method_decl)
922 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000923
Sean Callanan01c8cb82013-09-04 23:25:26 +0000924 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000925
Sean Callanan01c8cb82013-09-04 23:25:26 +0000926 if (log)
927 {
928 ASTDumper dumper((Decl*)copied_method_decl);
929 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
930 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000931
Sean Callanan01c8cb82013-09-04 23:25:26 +0000932 context.AddNamedDecl(copied_method_decl);
Sean Callananbc47dfc2012-09-11 21:44:01 +0000933 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000934
Sean Callananc83e3412012-11-28 03:23:20 +0000935 return true;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000936}
937
Sean Callanan0730e9c2011-11-09 19:33:21 +0000938void
939ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
940{
Greg Clayton5160ce52013-03-27 23:08:40 +0000941 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000942
Sean Callanan46198ff2011-11-11 20:37:26 +0000943 static unsigned int invocation_id = 0;
944 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000945
Sean Callanan0730e9c2011-11-09 19:33:21 +0000946 const DeclarationName &decl_name(context.m_decl_name);
947 const DeclContext *decl_ctx(context.m_decl_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000948
Sean Callanan0730e9c2011-11-09 19:33:21 +0000949 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000950
Sean Callanan0730e9c2011-11-09 19:33:21 +0000951 if (!interface_decl)
952 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000953
Sean Callanana6582262012-04-05 00:12:52 +0000954 do
955 {
956 Decl *original_decl = NULL;
957 ASTContext *original_ctx = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000958
Sean Callanana6582262012-04-05 00:12:52 +0000959 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000960
Sean Callanana6582262012-04-05 00:12:52 +0000961 if (!original_decl)
962 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000963
Sean Callanana6582262012-04-05 00:12:52 +0000964 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000965
Sean Callananc83e3412012-11-28 03:23:20 +0000966 if (FindObjCMethodDeclsWithOrigin(current_id,
967 context,
968 original_interface_decl,
969 m_ast_context,
970 m_ast_importer,
971 "at origin"))
972 return; // found it, no need to look any further
Sean Callanana6582262012-04-05 00:12:52 +0000973 } while (0);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000974
Sean Callanan0730e9c2011-11-09 19:33:21 +0000975 StreamString ss;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000976
Sean Callanan0730e9c2011-11-09 19:33:21 +0000977 if (decl_name.isObjCZeroArgSelector())
978 {
979 ss.Printf("%s", decl_name.getAsString().c_str());
980 }
981 else if (decl_name.isObjCOneArgSelector())
982 {
Sean Callanan4fb79b72011-11-14 18:29:46 +0000983 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan0730e9c2011-11-09 19:33:21 +0000984 }
985 else
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000986 {
Sean Callanan0730e9c2011-11-09 19:33:21 +0000987 clang::Selector sel = decl_name.getObjCSelector();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000988
Sean Callanan0730e9c2011-11-09 19:33:21 +0000989 for (unsigned i = 0, e = sel.getNumArgs();
990 i != e;
991 ++i)
992 {
993 llvm::StringRef r = sel.getNameForSlot(i);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000994 ss.Printf("%s:", r.str().c_str());
Sean Callanan0730e9c2011-11-09 19:33:21 +0000995 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000996 }
Sean Callanan0730e9c2011-11-09 19:33:21 +0000997 ss.Flush();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000998
Sean Callanand6be7082014-02-06 22:24:11 +0000999 if (strstr(ss.GetData(), "$__lldb"))
1000 return; // we don't need any results
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001001
Sean Callanan46198ff2011-11-11 20:37:26 +00001002 ConstString selector_name(ss.GetData());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001003
Sean Callanan0730e9c2011-11-09 19:33:21 +00001004 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +00001005 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001006 current_id, static_cast<void*>(m_ast_context),
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001007 interface_decl->getNameAsString().c_str(),
Sean Callanan46198ff2011-11-11 20:37:26 +00001008 selector_name.AsCString());
Sean Callanan46198ff2011-11-11 20:37:26 +00001009 SymbolContextList sc_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001010
Sean Callanan46198ff2011-11-11 20:37:26 +00001011 const bool include_symbols = false;
Sean Callanan9df05fb2012-02-10 22:52:19 +00001012 const bool include_inlines = false;
Sean Callanan46198ff2011-11-11 20:37:26 +00001013 const bool append = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001014
Sean Callanana9bc0652012-01-19 02:17:40 +00001015 std::string interface_name = interface_decl->getNameAsString();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001016
Sean Callanana9bc0652012-01-19 02:17:40 +00001017 do
1018 {
1019 StreamString ms;
1020 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
1021 ms.Flush();
1022 ConstString instance_method_name(ms.GetData());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001023
Sean Callanan9df05fb2012-02-10 22:52:19 +00001024 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001025
Sean Callanana9bc0652012-01-19 02:17:40 +00001026 if (sc_list.GetSize())
1027 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001028
Sean Callanana9bc0652012-01-19 02:17:40 +00001029 ms.Clear();
1030 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
1031 ms.Flush();
1032 ConstString class_method_name(ms.GetData());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001033
Sean Callanan9df05fb2012-02-10 22:52:19 +00001034 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001035
Sean Callanana9bc0652012-01-19 02:17:40 +00001036 if (sc_list.GetSize())
1037 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001038
Sean Callanana9bc0652012-01-19 02:17:40 +00001039 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
1040 // categories on the desired class.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001041
Sean Callanana9bc0652012-01-19 02:17:40 +00001042 SymbolContextList candidate_sc_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001043
Sean Callanan9df05fb2012-02-10 22:52:19 +00001044 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001045
Sean Callanana9bc0652012-01-19 02:17:40 +00001046 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
1047 ci != ce;
1048 ++ci)
1049 {
1050 SymbolContext candidate_sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001051
Sean Callanana9bc0652012-01-19 02:17:40 +00001052 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1053 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001054
Sean Callanana9bc0652012-01-19 02:17:40 +00001055 if (!candidate_sc.function)
1056 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001057
Sean Callanana9bc0652012-01-19 02:17:40 +00001058 const char *candidate_name = candidate_sc.function->GetName().AsCString();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001059
Sean Callanana9bc0652012-01-19 02:17:40 +00001060 const char *cursor = candidate_name;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001061
Sean Callanana9bc0652012-01-19 02:17:40 +00001062 if (*cursor != '+' && *cursor != '-')
1063 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001064
Sean Callanana9bc0652012-01-19 02:17:40 +00001065 ++cursor;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001066
Sean Callanana9bc0652012-01-19 02:17:40 +00001067 if (*cursor != '[')
1068 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001069
Sean Callanana9bc0652012-01-19 02:17:40 +00001070 ++cursor;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001071
Sean Callanana9bc0652012-01-19 02:17:40 +00001072 size_t interface_len = interface_name.length();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001073
Sean Callanana9bc0652012-01-19 02:17:40 +00001074 if (strncmp(cursor, interface_name.c_str(), interface_len))
1075 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001076
Sean Callanana9bc0652012-01-19 02:17:40 +00001077 cursor += interface_len;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001078
Sean Callanana9bc0652012-01-19 02:17:40 +00001079 if (*cursor == ' ' || *cursor == '(')
1080 sc_list.Append(candidate_sc);
1081 }
1082 }
1083 while (0);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001084
Sean Callananbc47dfc2012-09-11 21:44:01 +00001085 if (sc_list.GetSize())
Sean Callanan46198ff2011-11-11 20:37:26 +00001086 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001087 // We found a good function symbol. Use that.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001088
Sean Callananbc47dfc2012-09-11 21:44:01 +00001089 for (uint32_t i = 0, e = sc_list.GetSize();
1090 i != e;
1091 ++i)
Sean Callanan46198ff2011-11-11 20:37:26 +00001092 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001093 SymbolContext sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001094
Sean Callananbc47dfc2012-09-11 21:44:01 +00001095 if (!sc_list.GetContextAtIndex(i, sc))
Sean Callanan46198ff2011-11-11 20:37:26 +00001096 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001097
Sean Callananbc47dfc2012-09-11 21:44:01 +00001098 if (!sc.function)
Sean Callanan46198ff2011-11-11 20:37:26 +00001099 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001100
Sean Callananbc47dfc2012-09-11 21:44:01 +00001101 DeclContext *function_ctx = sc.function->GetClangDeclContext();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001102
Sean Callananbc47dfc2012-09-11 21:44:01 +00001103 if (!function_ctx)
1104 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001105
Sean Callananbc47dfc2012-09-11 21:44:01 +00001106 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001107
Sean Callananbc47dfc2012-09-11 21:44:01 +00001108 if (!method_decl)
1109 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001110
Sean Callananbc47dfc2012-09-11 21:44:01 +00001111 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001112
Sean Callananbc47dfc2012-09-11 21:44:01 +00001113 if (!found_interface_decl)
1114 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001115
Sean Callananbc47dfc2012-09-11 21:44:01 +00001116 if (found_interface_decl->getName() == interface_decl->getName())
Sean Callanan46198ff2011-11-11 20:37:26 +00001117 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001118 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001119
Sean Callananbc47dfc2012-09-11 21:44:01 +00001120 if (!copied_decl)
1121 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001122
Sean Callananbc47dfc2012-09-11 21:44:01 +00001123 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001124
Sean Callananbc47dfc2012-09-11 21:44:01 +00001125 if (!copied_method_decl)
1126 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001127
Sean Callananbc47dfc2012-09-11 21:44:01 +00001128 if (log)
1129 {
1130 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanan55400942012-11-02 17:09:58 +00001131 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
Sean Callananbc47dfc2012-09-11 21:44:01 +00001132 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001133
Sean Callananbc47dfc2012-09-11 21:44:01 +00001134 context.AddNamedDecl(copied_method_decl);
Sean Callanan46198ff2011-11-11 20:37:26 +00001135 }
Sean Callanan46198ff2011-11-11 20:37:26 +00001136 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001137
Sean Callanan55400942012-11-02 17:09:58 +00001138 return;
Sean Callanan46198ff2011-11-11 20:37:26 +00001139 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001140
Sean Callanan55400942012-11-02 17:09:58 +00001141 // Try the debug information.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001142
Sean Callanan55400942012-11-02 17:09:58 +00001143 do
Sean Callananbc47dfc2012-09-11 21:44:01 +00001144 {
Sean Callanan55400942012-11-02 17:09:58 +00001145 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001146
Sean Callanan55400942012-11-02 17:09:58 +00001147 if (!complete_interface_decl)
1148 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001149
Sean Callanan55400942012-11-02 17:09:58 +00001150 // We found the complete interface. The runtime never needs to be queried in this scenario.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001151
Sean Callanan55400942012-11-02 17:09:58 +00001152 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001153
Sean Callanan55400942012-11-02 17:09:58 +00001154 if (complete_interface_decl == interface_decl)
1155 break; // already checked this one
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001156
Sean Callanan55400942012-11-02 17:09:58 +00001157 if (log)
1158 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001159 current_id, static_cast<void*>(complete_interface_decl),
1160 static_cast<void*>(&complete_iface_decl->getASTContext()));
1161
Sean Callanan55400942012-11-02 17:09:58 +00001162 FindObjCMethodDeclsWithOrigin(current_id,
1163 context,
1164 complete_interface_decl,
1165 m_ast_context,
1166 m_ast_importer,
1167 "in debug info");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001168
Sean Callanan55400942012-11-02 17:09:58 +00001169 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001170 }
Sean Callanan55400942012-11-02 17:09:58 +00001171 while (0);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001172
Sean Callanan55400942012-11-02 17:09:58 +00001173 do
1174 {
1175 // Check the runtime only if the debug information didn't have a complete interface.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001176
Sean Callanan55400942012-11-02 17:09:58 +00001177 lldb::ProcessSP process(m_target->GetProcessSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001178
Sean Callanan55400942012-11-02 17:09:58 +00001179 if (!process)
1180 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001181
Sean Callanan55400942012-11-02 17:09:58 +00001182 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001183
Sean Callanan7be70e82012-12-19 23:05:01 +00001184 if (!language_runtime)
1185 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001186
Sean Callanan55400942012-11-02 17:09:58 +00001187 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001188
Sean Callanan55400942012-11-02 17:09:58 +00001189 if (!type_vendor)
1190 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001191
Sean Callanan55400942012-11-02 17:09:58 +00001192 ConstString interface_name(interface_decl->getNameAsString().c_str());
1193 bool append = false;
1194 uint32_t max_matches = 1;
1195 std::vector <ClangASTType> types;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001196
Sean Callanan55400942012-11-02 17:09:58 +00001197 if (!type_vendor->FindTypes(interface_name,
1198 append,
1199 max_matches,
1200 types))
1201 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001202
Sean Callanan55400942012-11-02 17:09:58 +00001203 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001204
Sean Callanan55400942012-11-02 17:09:58 +00001205 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001206
Sean Callanan55400942012-11-02 17:09:58 +00001207 if (!runtime_interface_type)
1208 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001209
Sean Callanan55400942012-11-02 17:09:58 +00001210 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001211
Sean Callanan55400942012-11-02 17:09:58 +00001212 FindObjCMethodDeclsWithOrigin(current_id,
1213 context,
1214 runtime_interface_decl,
1215 m_ast_context,
1216 m_ast_importer,
1217 "in runtime");
1218 }
1219 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001220}
1221
Sean Callanan72772842012-02-22 23:57:45 +00001222static bool
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001223FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
Sean Callanan72772842012-02-22 23:57:45 +00001224 NameSearchContext &context,
1225 clang::ASTContext &ast_context,
1226 ClangASTImporter *ast_importer,
1227 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1228{
Greg Clayton5160ce52013-03-27 23:08:40 +00001229 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan72772842012-02-22 23:57:45 +00001230
1231 if (origin_iface_decl.IsInvalid())
1232 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001233
Sean Callanan72772842012-02-22 23:57:45 +00001234 std::string name_str = context.m_decl_name.getAsString();
1235 StringRef name(name_str.c_str());
1236 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001237
Sean Callanan72772842012-02-22 23:57:45 +00001238 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001239
Sean Callanan72772842012-02-22 23:57:45 +00001240 bool found = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001241
Sean Callanan72772842012-02-22 23:57:45 +00001242 if (origin_property_decl.IsValid())
1243 {
1244 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1245 if (parser_property_decl.IsValid())
1246 {
1247 if (log)
1248 {
1249 ASTDumper dumper((Decl*)parser_property_decl.decl);
1250 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1251 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001252
Sean Callanan72772842012-02-22 23:57:45 +00001253 context.AddNamedDecl(parser_property_decl.decl);
1254 found = true;
1255 }
1256 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001257
Sean Callanan72772842012-02-22 23:57:45 +00001258 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001259
Sean Callanan72772842012-02-22 23:57:45 +00001260 if (origin_ivar_decl.IsValid())
1261 {
1262 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1263 if (parser_ivar_decl.IsValid())
1264 {
1265 if (log)
1266 {
1267 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1268 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1269 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001270
Sean Callanan72772842012-02-22 23:57:45 +00001271 context.AddNamedDecl(parser_ivar_decl.decl);
1272 found = true;
1273 }
1274 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001275
Sean Callanan72772842012-02-22 23:57:45 +00001276 return found;
1277}
1278
Sean Callanand5c17ed2011-11-15 02:11:17 +00001279void
Sean Callanan5b26f272012-02-04 08:49:35 +00001280ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001281{
Greg Clayton5160ce52013-03-27 23:08:40 +00001282 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand5c17ed2011-11-15 02:11:17 +00001283
1284 static unsigned int invocation_id = 0;
1285 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001286
Sean Callanan5b26f272012-02-04 08:49:35 +00001287 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1288 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan72772842012-02-22 23:57:45 +00001289
1290 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001291
Sean Callanand5c17ed2011-11-15 02:11:17 +00001292 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00001293 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001294 current_id, static_cast<void*>(m_ast_context),
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001295 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan72772842012-02-22 23:57:45 +00001296 context.m_decl_name.getAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001297
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001298 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1299 context,
1300 *m_ast_context,
1301 m_ast_importer,
Sean Callanan72772842012-02-22 23:57:45 +00001302 origin_iface_decl))
1303 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001304
Sean Callanan72772842012-02-22 23:57:45 +00001305 if (log)
1306 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001307 current_id, static_cast<const void*>(origin_iface_decl.decl),
1308 static_cast<void*>(&origin_iface_decl->getASTContext()));
1309
Sean Callanan72772842012-02-22 23:57:45 +00001310 SymbolContext null_sc;
1311 TypeList type_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001312
Sean Callananbc47dfc2012-09-11 21:44:01 +00001313 do
1314 {
Sean Callanan2cb5e522012-09-20 23:21:16 +00001315 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001316
Sean Callanan2cb5e522012-09-20 23:21:16 +00001317 if (!complete_interface_decl)
Sean Callananbc47dfc2012-09-11 21:44:01 +00001318 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001319
Sean Callanan55400942012-11-02 17:09:58 +00001320 // We found the complete interface. The runtime never needs to be queried in this scenario.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001321
Sean Callanan2cb5e522012-09-20 23:21:16 +00001322 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001323
Sean Callananbc47dfc2012-09-11 21:44:01 +00001324 if (complete_iface_decl.decl == origin_iface_decl.decl)
1325 break; // already checked this one
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001326
Sean Callananbc47dfc2012-09-11 21:44:01 +00001327 if (log)
1328 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1329 current_id,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001330 static_cast<const void*>(complete_iface_decl.decl),
1331 static_cast<void*>(&complete_iface_decl->getASTContext()));
1332
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001333 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1334 context,
1335 *m_ast_context,
1336 m_ast_importer,
Sean Callanan55400942012-11-02 17:09:58 +00001337 complete_iface_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001338
Sean Callanan55400942012-11-02 17:09:58 +00001339 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001340 }
1341 while(0);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001342
Sean Callananbc47dfc2012-09-11 21:44:01 +00001343 do
1344 {
Sean Callanan55400942012-11-02 17:09:58 +00001345 // Check the runtime only if the debug information didn't have a complete interface.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001346
Sean Callanan55400942012-11-02 17:09:58 +00001347 lldb::ProcessSP process(m_target->GetProcessSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001348
Sean Callanan55400942012-11-02 17:09:58 +00001349 if (!process)
1350 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001351
Sean Callanan55400942012-11-02 17:09:58 +00001352 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001353
Sean Callanan55400942012-11-02 17:09:58 +00001354 if (!language_runtime)
1355 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001356
Sean Callananbc47dfc2012-09-11 21:44:01 +00001357 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001358
Sean Callananbc47dfc2012-09-11 21:44:01 +00001359 if (!type_vendor)
1360 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001361
Sean Callananbc47dfc2012-09-11 21:44:01 +00001362 bool append = false;
1363 uint32_t max_matches = 1;
1364 std::vector <ClangASTType> types;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001365
Sean Callananbc47dfc2012-09-11 21:44:01 +00001366 if (!type_vendor->FindTypes(class_name,
1367 append,
1368 max_matches,
1369 types))
1370 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001371
Sean Callananbc47dfc2012-09-11 21:44:01 +00001372 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1373
1374 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001375
Sean Callananbc47dfc2012-09-11 21:44:01 +00001376 if (!runtime_interface_type)
1377 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001378
Sean Callananbc47dfc2012-09-11 21:44:01 +00001379 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001380
Sean Callananbc47dfc2012-09-11 21:44:01 +00001381 if (log)
1382 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1383 current_id,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001384 static_cast<const void*>(runtime_iface_decl.decl),
1385 static_cast<void*>(&runtime_iface_decl->getASTContext()));
1386
Sean Callananbc47dfc2012-09-11 21:44:01 +00001387 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1388 context,
1389 *m_ast_context,
1390 m_ast_importer,
1391 runtime_iface_decl))
1392 return;
1393 }
1394 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001395}
1396
1397typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1398typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1399
1400template <class D, class O>
1401static bool
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001402ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
Sean Callanan5b26f272012-02-04 08:49:35 +00001403 llvm::DenseMap <const D*, O> &source_map,
1404 ClangASTImporter *importer,
1405 ASTContext &dest_ctx)
1406{
1407 typedef llvm::DenseMap <const D*, O> MapType;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001408
Sean Callanan5b26f272012-02-04 08:49:35 +00001409 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1410 fi != fe;
1411 ++fi)
1412 {
1413 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1414 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1415 if (parser_decl.IsInvalid())
1416 return false;
1417 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1418 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001419
Sean Callanan5b26f272012-02-04 08:49:35 +00001420 return true;
1421}
1422
1423template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1424 DeclFromUser<const CXXRecordDecl> &record,
1425 BaseOffsetMap &base_offsets)
1426{
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001427 for (CXXRecordDecl::base_class_const_iterator
1428 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
Sean Callanan5b26f272012-02-04 08:49:35 +00001429 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1430 bi != be;
1431 ++bi)
1432 {
1433 if (!IsVirtual && bi->isVirtual())
1434 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001435
Sean Callanan5b26f272012-02-04 08:49:35 +00001436 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1437 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001438
Sean Callanan5b26f272012-02-04 08:49:35 +00001439 if (!origin_base_record_type)
1440 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001441
Sean Callanan5b26f272012-02-04 08:49:35 +00001442 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001443
Sean Callanan5b26f272012-02-04 08:49:35 +00001444 if (origin_base_record.IsInvalid())
1445 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001446
Sean Callanan5b26f272012-02-04 08:49:35 +00001447 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001448
Sean Callanan5b26f272012-02-04 08:49:35 +00001449 if (origin_base_cxx_record.IsInvalid())
1450 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001451
Sean Callanan5b26f272012-02-04 08:49:35 +00001452 CharUnits base_offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001453
Sean Callanan5b26f272012-02-04 08:49:35 +00001454 if (IsVirtual)
1455 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1456 else
1457 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001458
Sean Callanan5b26f272012-02-04 08:49:35 +00001459 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1460 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001461
Sean Callanan5b26f272012-02-04 08:49:35 +00001462 return true;
1463}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001464
1465bool
Sean Callanan5b26f272012-02-04 08:49:35 +00001466ClangASTSource::layoutRecordType(const RecordDecl *record,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001467 uint64_t &size,
Sean Callanan5b26f272012-02-04 08:49:35 +00001468 uint64_t &alignment,
1469 FieldOffsetMap &field_offsets,
1470 BaseOffsetMap &base_offsets,
1471 BaseOffsetMap &virtual_base_offsets)
1472{
Sean Callanan8106d802013-03-08 20:04:57 +00001473 ClangASTMetrics::RegisterRecordLayout();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001474
Sean Callanan5b26f272012-02-04 08:49:35 +00001475 static unsigned int invocation_id = 0;
1476 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001477
Greg Clayton5160ce52013-03-27 23:08:40 +00001478 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001479
Sean Callanand5c17ed2011-11-15 02:11:17 +00001480 if (log)
Sean Callanan933ca2e2013-02-28 03:12:58 +00001481 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p [name = '%s']",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001482 current_id, static_cast<void*>(m_ast_context),
1483 static_cast<const void*>(record),
Sean Callanan5b26f272012-02-04 08:49:35 +00001484 record->getNameAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001485
Sean Callanan5b26f272012-02-04 08:49:35 +00001486 DeclFromParser <const RecordDecl> parser_record(record);
1487 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001488
Sean Callanan5b26f272012-02-04 08:49:35 +00001489 if (origin_record.IsInvalid())
1490 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001491
Sean Callanan5b26f272012-02-04 08:49:35 +00001492 FieldOffsetMap origin_field_offsets;
1493 BaseOffsetMap origin_base_offsets;
1494 BaseOffsetMap origin_virtual_base_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001495
Sean Callananbf81ea52012-04-07 00:06:00 +00001496 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001497
Sean Callananbf81ea52012-04-07 00:06:00 +00001498 if (!origin_record.decl->getDefinition())
1499 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001500
Sean Callanan5b26f272012-02-04 08:49:35 +00001501 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001502
Sean Callananeb7b27d2013-03-25 18:27:07 +00001503 int field_idx = 0, field_count = record_layout.getFieldCount();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001504
Sean Callanan5b26f272012-02-04 08:49:35 +00001505 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1506 fi != fe;
1507 ++fi)
1508 {
Sean Callananeb7b27d2013-03-25 18:27:07 +00001509 if (field_idx >= field_count)
1510 return false; // Layout didn't go well. Bail out.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001511
Sean Callanan5b26f272012-02-04 08:49:35 +00001512 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001513
Sean Callanan5b26f272012-02-04 08:49:35 +00001514 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001515
Sean Callanan5b26f272012-02-04 08:49:35 +00001516 field_idx++;
1517 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001518
Sean Callanan5b26f272012-02-04 08:49:35 +00001519 ASTContext &parser_ast_context(record->getASTContext());
1520
1521 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1522
1523 if (origin_cxx_record.IsValid())
1524 {
1525 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1526 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1527 return false;
1528 }
1529
1530 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1531 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1532 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1533 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001534
Sean Callanan5b26f272012-02-04 08:49:35 +00001535 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1536 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001537
Sean Callanan5b26f272012-02-04 08:49:35 +00001538 if (log)
1539 {
1540 log->Printf("LRT[%u] returned:", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001541 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id,
1542 static_cast<const void*>(origin_record.decl));
Daniel Malead01b2952012-11-29 21:49:15 +00001543 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1544 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
Sean Callanan5b26f272012-02-04 08:49:35 +00001545 log->Printf("LRT[%u] Fields:", current_id);
1546 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1547 fi != fe;
1548 ++fi)
1549 {
Daniel Malead01b2952012-11-29 21:49:15 +00001550 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001551 current_id, static_cast<void*>(*fi),
1552 fi->getNameAsString().c_str(), field_offsets[*fi]);
Sean Callanan5b26f272012-02-04 08:49:35 +00001553 }
1554 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1555 if (parser_cxx_record.IsValid())
1556 {
1557 log->Printf("LRT[%u] Bases:", current_id);
1558 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1559 bi != be;
1560 ++bi)
1561 {
1562 bool is_virtual = bi->isVirtual();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001563
Sean Callanan5b26f272012-02-04 08:49:35 +00001564 QualType base_type = bi->getType();
1565 const RecordType *base_record_type = base_type->getAs<RecordType>();
1566 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1567 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001568
Daniel Malead01b2952012-11-29 21:49:15 +00001569 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001570 current_id, (is_virtual ? "Virtual " : ""),
1571 static_cast<void*>(base_cxx_record.decl),
Sean Callanan5b26f272012-02-04 08:49:35 +00001572 base_cxx_record.decl->getNameAsString().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001573 (is_virtual
1574 ? virtual_base_offsets[base_cxx_record.decl].getQuantity()
1575 : base_offsets[base_cxx_record.decl].getQuantity()));
Sean Callanan5b26f272012-02-04 08:49:35 +00001576 }
1577 }
1578 else
1579 {
1580 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1581 }
1582 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001583
Sean Callanan5b26f272012-02-04 08:49:35 +00001584 return true;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001585}
1586
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001587void
Sean Callanan1ee44b72011-10-29 01:58:46 +00001588ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1589 const ConstString &name,
1590 ClangASTImporter::NamespaceMapSP &parent_map) const
1591{
1592 static unsigned int invocation_id = 0;
1593 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001594
Greg Clayton5160ce52013-03-27 23:08:40 +00001595 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001596
Sean Callanan1ee44b72011-10-29 01:58:46 +00001597 if (log)
1598 {
1599 if (parent_map && parent_map->size())
Sean Callanan00f43622011-11-18 03:28:09 +00001600 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001601 current_id, static_cast<void*>(m_ast_context),
Sean Callanan1ee44b72011-10-29 01:58:46 +00001602 name.GetCString(),
1603 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1604 else
Sean Callanan00f43622011-11-18 03:28:09 +00001605 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001606 current_id, static_cast<void*>(m_ast_context),
Sean Callanan1ee44b72011-10-29 01:58:46 +00001607 name.GetCString());
1608 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001609
Sean Callanan1ee44b72011-10-29 01:58:46 +00001610 if (parent_map)
1611 {
1612 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1613 i != e;
1614 ++i)
1615 {
1616 ClangNamespaceDecl found_namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001617
Sean Callanan1ee44b72011-10-29 01:58:46 +00001618 lldb::ModuleSP module_sp = i->first;
1619 ClangNamespaceDecl module_parent_namespace_decl = i->second;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001620
Sean Callanan1ee44b72011-10-29 01:58:46 +00001621 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001622
Sean Callanan1ee44b72011-10-29 01:58:46 +00001623 if (!symbol_vendor)
1624 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001625
Sean Callanan1ee44b72011-10-29 01:58:46 +00001626 SymbolContext null_sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001627
Sean Callanan1ee44b72011-10-29 01:58:46 +00001628 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001629
Sean Callanan1ee44b72011-10-29 01:58:46 +00001630 if (!found_namespace_decl)
1631 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001632
Sean Callanan1ee44b72011-10-29 01:58:46 +00001633 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001634
Sean Callanan1ee44b72011-10-29 01:58:46 +00001635 if (log)
1636 log->Printf(" CMN[%u] Found namespace %s in module %s",
1637 current_id,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001638 name.GetCString(),
Sean Callanan1ee44b72011-10-29 01:58:46 +00001639 module_sp->GetFileSpec().GetFilename().GetCString());
1640 }
1641 }
1642 else
1643 {
Enrico Granata17598482012-11-08 02:22:02 +00001644 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001645 Mutex::Locker modules_locker(target_images.GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001646
Sean Callanan1ee44b72011-10-29 01:58:46 +00001647 ClangNamespaceDecl null_namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001648
Greg Claytonc7bece562013-01-25 18:06:21 +00001649 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callanan1ee44b72011-10-29 01:58:46 +00001650 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001651 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001652
Sean Callanan1ee44b72011-10-29 01:58:46 +00001653 if (!image)
1654 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001655
Sean Callanan1ee44b72011-10-29 01:58:46 +00001656 ClangNamespaceDecl found_namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001657
Sean Callanan1ee44b72011-10-29 01:58:46 +00001658 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001659
Sean Callanan1ee44b72011-10-29 01:58:46 +00001660 if (!symbol_vendor)
1661 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001662
Sean Callanan1ee44b72011-10-29 01:58:46 +00001663 SymbolContext null_sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001664
Sean Callanan1ee44b72011-10-29 01:58:46 +00001665 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001666
Sean Callanan1ee44b72011-10-29 01:58:46 +00001667 if (!found_namespace_decl)
1668 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001669
Sean Callanan1ee44b72011-10-29 01:58:46 +00001670 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001671
Sean Callanan1ee44b72011-10-29 01:58:46 +00001672 if (log)
1673 log->Printf(" CMN[%u] Found namespace %s in module %s",
1674 current_id,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001675 name.GetCString(),
Sean Callanan1ee44b72011-10-29 01:58:46 +00001676 image->GetFileSpec().GetFilename().GetCString());
1677 }
1678 }
1679}
1680
Sean Callananba0aca72011-10-29 02:28:18 +00001681NamespaceDecl *
1682ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1683{
Greg Claytone1cd1be2012-01-29 20:56:30 +00001684 if (!namespace_decls)
Sean Callananba0aca72011-10-29 02:28:18 +00001685 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001686
Sean Callananba0aca72011-10-29 02:28:18 +00001687 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001688
Sean Callanan686b2312011-11-16 18:20:47 +00001689 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001690
Sean Callanan61b33f72012-03-20 21:11:12 +00001691 if (!copied_decl)
1692 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001693
Sean Callananba0aca72011-10-29 02:28:18 +00001694 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001695
Sean Callananeeffea42013-02-12 08:01:13 +00001696 if (!copied_namespace_decl)
1697 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001698
Sean Callananeeffea42013-02-12 08:01:13 +00001699 context.m_decls.push_back(copied_namespace_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001700
Sean Callananba0aca72011-10-29 02:28:18 +00001701 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001702
Sean Callananba0aca72011-10-29 02:28:18 +00001703 return dyn_cast<NamespaceDecl>(copied_decl);
1704}
1705
Greg Clayton57ee3062013-07-11 22:46:58 +00001706ClangASTType
1707ClangASTSource::GuardedCopyType (const ClangASTType &src_type)
Sean Callanan8106d802013-03-08 20:04:57 +00001708{
1709 ClangASTMetrics::RegisterLLDBImport();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001710
Sean Callananfb3e4302011-10-29 19:50:43 +00001711 SetImportInProgress(true);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001712
Greg Clayton57ee3062013-07-11 22:46:58 +00001713 QualType copied_qual_type = m_ast_importer->CopyType (m_ast_context, src_type.GetASTContext(), src_type.GetQualType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001714
Sean Callananfb3e4302011-10-29 19:50:43 +00001715 SetImportInProgress(false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001716
Greg Clayton57ee3062013-07-11 22:46:58 +00001717 if (copied_qual_type.getAsOpaquePtr() && copied_qual_type->getCanonicalTypeInternal().isNull())
Sean Callanan8b665982012-02-27 19:22:39 +00001718 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1719 // on occasion.
Greg Clayton57ee3062013-07-11 22:46:58 +00001720 return ClangASTType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001721
Greg Clayton57ee3062013-07-11 22:46:58 +00001722 return ClangASTType(m_ast_context, copied_qual_type);
Sean Callananfb3e4302011-10-29 19:50:43 +00001723}
1724
Greg Clayton6beaaa62011-01-17 03:46:26 +00001725clang::NamedDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001726NameSearchContext::AddVarDecl(const ClangASTType &type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001727{
Greg Clayton57ee3062013-07-11 22:46:58 +00001728 assert (type && "Type for variable must be valid!");
1729
1730 if (!type.IsValid())
1731 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001732
Greg Clayton7b462cc2010-10-15 22:48:33 +00001733 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001734
Greg Clayton57ee3062013-07-11 22:46:58 +00001735 clang::ASTContext *ast = type.GetASTContext();
1736
1737 clang::NamedDecl *Decl = VarDecl::Create(*ast,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001738 const_cast<DeclContext*>(m_decl_context),
Sean Callananfb0b7582011-03-15 00:17:19 +00001739 SourceLocation(),
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001740 SourceLocation(),
1741 ii,
Greg Clayton57ee3062013-07-11 22:46:58 +00001742 type.GetQualType(),
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001743 0,
Sean Callanane2ef6e32010-09-23 03:01:22 +00001744 SC_Static);
Greg Clayton7b462cc2010-10-15 22:48:33 +00001745 m_decls.push_back(Decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001746
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001747 return Decl;
1748}
Sean Callanan468574b2010-06-22 23:46:24 +00001749
Greg Clayton6beaaa62011-01-17 03:46:26 +00001750clang::NamedDecl *
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001751NameSearchContext::AddFunDecl (const ClangASTType &type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001752{
Greg Clayton57ee3062013-07-11 22:46:58 +00001753 assert (type && "Type for variable must be valid!");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001754
Greg Clayton57ee3062013-07-11 22:46:58 +00001755 if (!type.IsValid())
1756 return NULL;
1757
Sean Callanan485f7322013-04-24 00:34:41 +00001758 if (m_function_types.count(type))
1759 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001760
Sean Callanan485f7322013-04-24 00:34:41 +00001761 m_function_types.insert(type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001762
Greg Clayton57ee3062013-07-11 22:46:58 +00001763 QualType qual_type (type.GetQualType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001764
Greg Clayton57ee3062013-07-11 22:46:58 +00001765 clang::ASTContext *ast = type.GetASTContext();
1766
Greg Clayton0d551042013-06-28 21:08:47 +00001767 const bool isInlineSpecified = false;
1768 const bool hasWrittenPrototype = true;
1769 const bool isConstexprSpecified = false;
1770
Greg Clayton57ee3062013-07-11 22:46:58 +00001771 clang::FunctionDecl *func_decl = FunctionDecl::Create (*ast,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001772 const_cast<DeclContext*>(m_decl_context),
1773 SourceLocation(),
Jim Ingham6843e592013-06-28 22:21:22 +00001774 SourceLocation(),
Greg Clayton7b462cc2010-10-15 22:48:33 +00001775 m_decl_name.getAsIdentifierInfo(),
Greg Clayton57ee3062013-07-11 22:46:58 +00001776 qual_type,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001777 NULL,
1778 SC_Static,
Greg Clayton0d551042013-06-28 21:08:47 +00001779 isInlineSpecified,
1780 hasWrittenPrototype,
1781 isConstexprSpecified);
1782
Sean Callanan04949cc2010-08-12 23:45:38 +00001783 // We have to do more than just synthesize the FunctionDecl. We have to
1784 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1785 // this, we raid the function's FunctionProtoType for types.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001786
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001787 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001788
Greg Clayton7b462cc2010-10-15 22:48:33 +00001789 if (func_proto_type)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001790 {
Alp Tokerf747ff12014-01-20 21:14:57 +00001791 unsigned NumArgs = func_proto_type->getNumParams();
Sean Callanan468574b2010-06-22 23:46:24 +00001792 unsigned ArgIndex;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001793
Sean Callanan880e6802011-10-07 23:18:13 +00001794 SmallVector<ParmVarDecl *, 5> parm_var_decls;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001795
Sean Callanan468574b2010-06-22 23:46:24 +00001796 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1797 {
Alp Tokerf747ff12014-01-20 21:14:57 +00001798 QualType arg_qual_type (func_proto_type->getParamType(ArgIndex));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001799
Greg Clayton57ee3062013-07-11 22:46:58 +00001800 parm_var_decls.push_back(ParmVarDecl::Create (*ast,
Sean Callanan880e6802011-10-07 23:18:13 +00001801 const_cast<DeclContext*>(m_decl_context),
1802 SourceLocation(),
1803 SourceLocation(),
1804 NULL,
1805 arg_qual_type,
1806 NULL,
1807 SC_Static,
Sean Callanan880e6802011-10-07 23:18:13 +00001808 NULL));
Sean Callanan468574b2010-06-22 23:46:24 +00001809 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001810
Sean Callanan880e6802011-10-07 23:18:13 +00001811 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan468574b2010-06-22 23:46:24 +00001812 }
Sean Callanane0a64f72011-12-01 21:04:37 +00001813 else
1814 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001815 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane0a64f72011-12-01 21:04:37 +00001816
Sean Callanan8ef19772013-04-25 18:50:43 +00001817 if (log)
1818 log->Printf("Function type wasn't a FunctionProtoType");
Sean Callanane0a64f72011-12-01 21:04:37 +00001819 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001820
Greg Clayton7b462cc2010-10-15 22:48:33 +00001821 m_decls.push_back(func_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001822
Greg Clayton7b462cc2010-10-15 22:48:33 +00001823 return func_decl;
Sean Callanan468574b2010-06-22 23:46:24 +00001824}
Sean Callanan8ade1042010-07-27 00:55:47 +00001825
Greg Clayton6beaaa62011-01-17 03:46:26 +00001826clang::NamedDecl *
1827NameSearchContext::AddGenericFunDecl()
Sean Callanan8ade1042010-07-27 00:55:47 +00001828{
Sean Callanan2c777c42011-01-18 23:32:05 +00001829 FunctionProtoType::ExtProtoInfo proto_info;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001830
Sean Callanan2c777c42011-01-18 23:32:05 +00001831 proto_info.Variadic = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001832
Sean Callananeddeb3b2011-10-28 23:38:38 +00001833 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
Sylvestre Ledru1573b1b2013-03-10 20:13:16 +00001834 ArrayRef<QualType>(), // argument types
Sean Callananeddeb3b2011-10-28 23:38:38 +00001835 proto_info));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001836
Greg Clayton57ee3062013-07-11 22:46:58 +00001837 return AddFunDecl(ClangASTType (m_ast_source.m_ast_context, generic_function_type));
Sean Callanan8ade1042010-07-27 00:55:47 +00001838}
Sean Callanan5666b672010-08-04 01:02:13 +00001839
Greg Clayton6beaaa62011-01-17 03:46:26 +00001840clang::NamedDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001841NameSearchContext::AddTypeDecl(const ClangASTType &clang_type)
Sean Callanan5666b672010-08-04 01:02:13 +00001842{
Greg Clayton57ee3062013-07-11 22:46:58 +00001843 if (clang_type)
Greg Clayton1b03cb52011-01-23 00:34:52 +00001844 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001845 QualType qual_type = clang_type.GetQualType();
Sean Callanan5666b672010-08-04 01:02:13 +00001846
Sean Callanan6b2f22d2013-06-25 22:36:17 +00001847 if (const TypedefType *typedef_type = llvm::dyn_cast<TypedefType>(qual_type))
1848 {
1849 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001850
Sean Callanan6b2f22d2013-06-25 22:36:17 +00001851 m_decls.push_back(typedef_name_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001852
Sean Callanan6b2f22d2013-06-25 22:36:17 +00001853 return (NamedDecl*)typedef_name_decl;
1854 }
1855 else if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001856 {
1857 TagDecl *tag_decl = tag_type->getDecl();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001858
Greg Clayton1b03cb52011-01-23 00:34:52 +00001859 m_decls.push_back(tag_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001860
Greg Clayton1b03cb52011-01-23 00:34:52 +00001861 return tag_decl;
1862 }
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001863 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001864 {
1865 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001866
Greg Clayton1b03cb52011-01-23 00:34:52 +00001867 m_decls.push_back((NamedDecl*)interface_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001868
Greg Clayton1b03cb52011-01-23 00:34:52 +00001869 return (NamedDecl*)interface_decl;
1870 }
Sean Callanan5666b672010-08-04 01:02:13 +00001871 }
Greg Clayton1b03cb52011-01-23 00:34:52 +00001872 return NULL;
Sean Callanan5666b672010-08-04 01:02:13 +00001873}
Greg Claytona2721472011-06-25 00:44:06 +00001874
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001875void
Greg Claytona2721472011-06-25 00:44:06 +00001876NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1877{
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001878 for (clang::NamedDecl *decl : result)
1879 m_decls.push_back (decl);
Greg Claytona2721472011-06-25 00:44:06 +00001880}
1881
1882void
1883NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1884{
1885 m_decls.push_back (decl);
1886}