blob: debd05324d859662ff4a043239db4340538651cb [file] [log] [blame]
Greg Claytona2721472011-06-25 00:44:06 +00001//===-- ClangASTImporter.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
10#include "clang/AST/Decl.h"
Sean Callanancbbe3ac2012-01-13 22:55:55 +000011#include "clang/AST/DeclCXX.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000012#include "clang/AST/DeclObjC.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000013#include "llvm/Support/raw_ostream.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000014#include "lldb/Core/Log.h"
Greg Clayton7c6d7b82011-10-21 23:04:20 +000015#include "lldb/Core/Module.h"
Greg Claytona2721472011-06-25 00:44:06 +000016#include "lldb/Symbol/ClangASTContext.h"
17#include "lldb/Symbol/ClangASTImporter.h"
Sean Callanan60217122012-04-13 00:10:03 +000018#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callananb2269162011-10-21 22:18:07 +000019#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Claytona2721472011-06-25 00:44:06 +000020
21using namespace lldb_private;
22using namespace clang;
23
Sean Callanan8106d802013-03-08 20:04:57 +000024ClangASTMetrics::Counters ClangASTMetrics::global_counters = { 0, 0, 0, 0, 0, 0 };
25ClangASTMetrics::Counters ClangASTMetrics::local_counters = { 0, 0, 0, 0, 0, 0 };
26
Greg Clayton5160ce52013-03-27 23:08:40 +000027void ClangASTMetrics::DumpCounters (Log *log, ClangASTMetrics::Counters &counters)
Sean Callanan8106d802013-03-08 20:04:57 +000028{
Matt Kopec787d1622013-03-12 17:45:38 +000029 log->Printf(" Number of visible Decl queries by name : %" PRIu64, counters.m_visible_query_count);
30 log->Printf(" Number of lexical Decl queries : %" PRIu64, counters.m_lexical_query_count);
31 log->Printf(" Number of imports initiated by LLDB : %" PRIu64, counters.m_lldb_import_count);
32 log->Printf(" Number of imports conducted by Clang : %" PRIu64, counters.m_clang_import_count);
33 log->Printf(" Number of Decls completed : %" PRIu64, counters.m_decls_completed_count);
34 log->Printf(" Number of records laid out : %" PRIu64, counters.m_record_layout_count);
Sean Callanan8106d802013-03-08 20:04:57 +000035}
36
Greg Clayton5160ce52013-03-27 23:08:40 +000037void ClangASTMetrics::DumpCounters (Log *log)
Sean Callanan8106d802013-03-08 20:04:57 +000038{
39 if (!log)
40 return;
41
42 log->Printf("== ClangASTMetrics output ==");
43 log->Printf("-- Global metrics --");
44 DumpCounters (log, global_counters);
45 log->Printf("-- Local metrics --");
46 DumpCounters (log, local_counters);
47}
48
49clang::QualType
Sean Callanan686b2312011-11-16 18:20:47 +000050ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
51 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000052 clang::QualType type)
53{
Sean Callanan686b2312011-11-16 18:20:47 +000054 MinionSP minion_sp (GetMinion(dst_ast, src_ast));
Sean Callanancc427fa2011-07-30 02:42:06 +000055
Greg Claytondd0649b2011-07-06 18:55:08 +000056 if (minion_sp)
57 return minion_sp->Import(type);
Sean Callanancc427fa2011-07-30 02:42:06 +000058
Greg Claytondd0649b2011-07-06 18:55:08 +000059 return QualType();
Greg Claytona2721472011-06-25 00:44:06 +000060}
61
Sean Callanan80f78672011-11-16 19:07:39 +000062lldb::clang_type_t
63ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
64 clang::ASTContext *src_ast,
65 lldb::clang_type_t type)
66{
67 return CopyType (dst_ast, src_ast, QualType::getFromOpaquePtr(type)).getAsOpaquePtr();
68}
69
Greg Claytona2721472011-06-25 00:44:06 +000070clang::Decl *
Sean Callanan686b2312011-11-16 18:20:47 +000071ClangASTImporter::CopyDecl (clang::ASTContext *dst_ast,
72 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000073 clang::Decl *decl)
74{
Greg Claytondd0649b2011-07-06 18:55:08 +000075 MinionSP minion_sp;
Greg Claytona2721472011-06-25 00:44:06 +000076
Sean Callanan686b2312011-11-16 18:20:47 +000077 minion_sp = GetMinion(dst_ast, src_ast);
Greg Claytona2721472011-06-25 00:44:06 +000078
Greg Claytondd0649b2011-07-06 18:55:08 +000079 if (minion_sp)
Sean Callananbfb237bc2011-11-04 22:46:46 +000080 {
81 clang::Decl *result = minion_sp->Import(decl);
82
83 if (!result)
84 {
Greg Clayton5160ce52013-03-27 23:08:40 +000085 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananbfb237bc2011-11-04 22:46:46 +000086
Sean Callanand5145b32011-11-05 00:08:12 +000087 if (log)
88 {
Jim Ingham379397632012-10-27 02:54:13 +000089 lldb::user_id_t user_id;
90 ClangASTMetadata *metadata = GetDeclMetadata(decl);
91 if (metadata)
92 user_id = metadata->GetUserID();
93
Sean Callanand5145b32011-11-05 00:08:12 +000094 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
Daniel Malead01b2952012-11-29 21:49:15 +000095 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s', metadata 0x%" PRIx64,
Sean Callanand9804fb2012-04-17 22:30:04 +000096 decl->getDeclKindName(),
97 named_decl->getNameAsString().c_str(),
Jim Ingham379397632012-10-27 02:54:13 +000098 user_id);
Sean Callanand5145b32011-11-05 00:08:12 +000099 else
Daniel Malead01b2952012-11-29 21:49:15 +0000100 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s, metadata 0x%" PRIx64,
Sean Callanand9804fb2012-04-17 22:30:04 +0000101 decl->getDeclKindName(),
Jim Ingham379397632012-10-27 02:54:13 +0000102 user_id);
Sean Callanand5145b32011-11-05 00:08:12 +0000103 }
Sean Callananbfb237bc2011-11-04 22:46:46 +0000104 }
105
106 return result;
107 }
Sean Callanancc427fa2011-07-30 02:42:06 +0000108
Ed Masted4612ad2014-04-20 13:17:36 +0000109 return nullptr;
Greg Claytona2721472011-06-25 00:44:06 +0000110}
111
Sean Callananbb120042011-12-16 21:06:35 +0000112lldb::clang_type_t
113ClangASTImporter::DeportType (clang::ASTContext *dst_ctx,
114 clang::ASTContext *src_ctx,
115 lldb::clang_type_t type)
Sean Callanane55bc8a2013-03-30 02:31:21 +0000116{
117 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
118
119 if (!minion_sp)
Ed Masted4612ad2014-04-20 13:17:36 +0000120 return nullptr;
Sean Callanane55bc8a2013-03-30 02:31:21 +0000121
122 std::set<NamedDecl *> decls_to_deport;
123 std::set<NamedDecl *> decls_already_deported;
124
125 minion_sp->InitDeportWorkQueues(&decls_to_deport,
126 &decls_already_deported);
127
Sean Callananbb120042011-12-16 21:06:35 +0000128 lldb::clang_type_t result = CopyType(dst_ctx, src_ctx, type);
129
Sean Callanane55bc8a2013-03-30 02:31:21 +0000130 minion_sp->ExecuteDeportWorkQueues();
131
Sean Callananbb120042011-12-16 21:06:35 +0000132 if (!result)
Ed Masted4612ad2014-04-20 13:17:36 +0000133 return nullptr;
Sean Callananbb120042011-12-16 21:06:35 +0000134
Sean Callananbb120042011-12-16 21:06:35 +0000135 return result;
Sean Callanane55bc8a2013-03-30 02:31:21 +0000136
Sean Callananbb120042011-12-16 21:06:35 +0000137}
138
Sean Callanan0eed0d42011-12-06 03:41:14 +0000139clang::Decl *
140ClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx,
141 clang::ASTContext *src_ctx,
142 clang::Decl *decl)
143{
Greg Clayton5160ce52013-03-27 23:08:40 +0000144 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000145
Sean Callanan933ca2e2013-02-28 03:12:58 +0000146 if (log)
147 log->Printf(" [ClangASTImporter] DeportDecl called on (%sDecl*)%p from (ASTContext*)%p to (ASTContex*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000148 decl->getDeclKindName(), static_cast<void*>(decl),
149 static_cast<void*>(src_ctx),
150 static_cast<void*>(dst_ctx));
151
Sean Callanane55bc8a2013-03-30 02:31:21 +0000152 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000153
Sean Callanane55bc8a2013-03-30 02:31:21 +0000154 if (!minion_sp)
Ed Masted4612ad2014-04-20 13:17:36 +0000155 return nullptr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000156
Sean Callanane55bc8a2013-03-30 02:31:21 +0000157 std::set<NamedDecl *> decls_to_deport;
158 std::set<NamedDecl *> decls_already_deported;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000159
Sean Callanane55bc8a2013-03-30 02:31:21 +0000160 minion_sp->InitDeportWorkQueues(&decls_to_deport,
161 &decls_already_deported);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000162
Sean Callanan0eed0d42011-12-06 03:41:14 +0000163 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
Sean Callanane55bc8a2013-03-30 02:31:21 +0000164
165 minion_sp->ExecuteDeportWorkQueues();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000166
Sean Callanan0eed0d42011-12-06 03:41:14 +0000167 if (!result)
Ed Masted4612ad2014-04-20 13:17:36 +0000168 return nullptr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000169
Sean Callanan933ca2e2013-02-28 03:12:58 +0000170 if (log)
171 log->Printf(" [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000172 decl->getDeclKindName(), static_cast<void*>(decl),
173 result->getDeclKindName(), static_cast<void*>(result));
174
Sean Callanan0eed0d42011-12-06 03:41:14 +0000175 return result;
176}
177
Sean Callanan5b26f272012-02-04 08:49:35 +0000178void
179ClangASTImporter::CompleteDecl (clang::Decl *decl)
180{
Greg Clayton5160ce52013-03-27 23:08:40 +0000181 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5b26f272012-02-04 08:49:35 +0000182
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000183 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +0000184 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000185 decl->getDeclKindName(), static_cast<void*>(decl));
186
Sean Callanan5b26f272012-02-04 08:49:35 +0000187 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
188 {
189 if (!interface_decl->getDefinition())
190 {
191 interface_decl->startDefinition();
192 CompleteObjCInterfaceDecl(interface_decl);
193 }
194 }
Greg Clayton23f59502012-07-17 03:23:13 +0000195 else if (ObjCProtocolDecl *protocol_decl = dyn_cast<ObjCProtocolDecl>(decl))
Sean Callanan5b26f272012-02-04 08:49:35 +0000196 {
197 if (!protocol_decl->getDefinition())
198 protocol_decl->startDefinition();
199 }
200 else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
201 {
202 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined())
203 {
204 tag_decl->startDefinition();
205 CompleteTagDecl(tag_decl);
206 tag_decl->setCompleteDefinition(true);
207 }
208 }
Greg Clayton219cf312012-03-30 00:51:13 +0000209 else
210 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000211 assert (0 && "CompleteDecl called on a Decl that can't be completed");
212 }
213}
214
Sean Callanan12014a02011-12-08 23:45:45 +0000215bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000216ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Sean Callanan8106d802013-03-08 20:04:57 +0000217{
218 ClangASTMetrics::RegisterDeclCompletion();
219
Sean Callanancc427fa2011-07-30 02:42:06 +0000220 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +0000221
Sean Callanancc427fa2011-07-30 02:42:06 +0000222 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000223 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000224
Sean Callanancc427fa2011-07-30 02:42:06 +0000225 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000226 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000227
Sean Callanan686b2312011-11-16 18:20:47 +0000228 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
Greg Claytona2721472011-06-25 00:44:06 +0000229
Greg Claytondd0649b2011-07-06 18:55:08 +0000230 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000231 minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
232
Sean Callanan12014a02011-12-08 23:45:45 +0000233 return true;
Greg Claytona2721472011-06-25 00:44:06 +0000234}
Sean Callanancc427fa2011-07-30 02:42:06 +0000235
Sean Callanan12014a02011-12-08 23:45:45 +0000236bool
237ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
238{
Sean Callanan8106d802013-03-08 20:04:57 +0000239 ClangASTMetrics::RegisterDeclCompletion();
240
Sean Callanan12014a02011-12-08 23:45:45 +0000241 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
242
243 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
244 return false;
245
246 MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
247
248 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000249 minion_sp->ImportDefinitionTo(decl, origin_decl);
250
Sean Callanan12014a02011-12-08 23:45:45 +0000251 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
252
253 OriginMap &origins = context_md->m_origins;
254
255 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
256
257 return true;
258}
259
260bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000261ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
262{
Sean Callanan8106d802013-03-08 20:04:57 +0000263 ClangASTMetrics::RegisterDeclCompletion();
Sean Callanancc427fa2011-07-30 02:42:06 +0000264
Sean Callanancc427fa2011-07-30 02:42:06 +0000265 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
266
267 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000268 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000269
270 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000271 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000272
Sean Callanan686b2312011-11-16 18:20:47 +0000273 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000274
275 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000276 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
277
Sean Callanan12014a02011-12-08 23:45:45 +0000278 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000279}
280
Sean Callanan6b200d02013-03-21 22:15:41 +0000281bool
282ClangASTImporter::RequireCompleteType (clang::QualType type)
283{
284 if (type.isNull())
285 return false;
286
287 if (const TagType *tag_type = type->getAs<TagType>())
288 {
289 return CompleteTagDecl(tag_type->getDecl());
290 }
291 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>())
292 {
293 if (ObjCInterfaceDecl *objc_interface_decl = objc_object_type->getInterface())
294 return CompleteObjCInterfaceDecl(objc_interface_decl);
295 else
296 return false;
297 }
298 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
299 {
300 return RequireCompleteType(array_type->getElementType());
301 }
302 if (const AtomicType *atomic_type = type->getAs<AtomicType>())
303 {
304 return RequireCompleteType(atomic_type->getPointeeType());
305 }
306
307 return true;
308}
309
Jim Ingham379397632012-10-27 02:54:13 +0000310ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +0000311ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
312{
313 DeclOrigin decl_origin = GetDeclOrigin(decl);
314
315 if (decl_origin.Valid())
Greg Claytond0029442013-03-27 01:48:02 +0000316 return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
Sean Callanan60217122012-04-13 00:10:03 +0000317 else
Greg Claytond0029442013-03-27 01:48:02 +0000318 return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
Sean Callanan60217122012-04-13 00:10:03 +0000319}
320
Sean Callananf487bd82011-11-16 21:40:57 +0000321ClangASTImporter::DeclOrigin
322ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
323{
324 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
325
326 OriginMap &origins = context_md->m_origins;
327
328 OriginMap::iterator iter = origins.find(decl);
329
330 if (iter != origins.end())
331 return iter->second;
332 else
333 return DeclOrigin();
334}
335
Sean Callanan2cb5e522012-09-20 23:21:16 +0000336void
337ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
338{
339 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
340
341 OriginMap &origins = context_md->m_origins;
342
343 OriginMap::iterator iter = origins.find(decl);
344
345 if (iter != origins.end())
346 {
347 iter->second.decl = original_decl;
348 iter->second.ctx = &original_decl->getASTContext();
349 }
350 else
351 {
352 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
353 }
354}
355
356void
Sean Callanan503aa522011-10-12 00:12:34 +0000357ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
358 NamespaceMapSP &namespace_map)
359{
Sean Callananf487bd82011-11-16 21:40:57 +0000360 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
361
362 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000363}
364
365ClangASTImporter::NamespaceMapSP
366ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
367{
Sean Callananf487bd82011-11-16 21:40:57 +0000368 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
369
370 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000371
Sean Callananf487bd82011-11-16 21:40:57 +0000372 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
373
374 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000375 return iter->second;
376 else
377 return NamespaceMapSP();
378}
379
Sean Callananb2269162011-10-21 22:18:07 +0000380void
381ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
382{
Jim Ingham28eb5712012-10-12 17:34:26 +0000383 assert (decl);
Sean Callananf487bd82011-11-16 21:40:57 +0000384 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
385
Sean Callananb2269162011-10-21 22:18:07 +0000386 const DeclContext *parent_context = decl->getDeclContext();
387 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
388 NamespaceMapSP parent_map;
389
390 if (parent_namespace)
391 parent_map = GetNamespaceMap(parent_namespace);
392
393 NamespaceMapSP new_map;
394
395 new_map.reset(new NamespaceMap);
396
Sean Callananf487bd82011-11-16 21:40:57 +0000397 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000398 {
399 std::string namespace_string = decl->getDeclName().getAsString();
400
Sean Callananf487bd82011-11-16 21:40:57 +0000401 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000402 }
403
Sean Callanan0eed0d42011-12-06 03:41:14 +0000404 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000405}
406
Sean Callanan686b2312011-11-16 18:20:47 +0000407void
Sean Callanan99732312011-11-29 00:42:02 +0000408ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000409{
Greg Clayton5160ce52013-03-27 23:08:40 +0000410 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000411
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000412 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000413 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p",
414 static_cast<void*>(dst_ast));
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000415
Sean Callananf487bd82011-11-16 21:40:57 +0000416 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000417}
418
Sean Callanan99732312011-11-29 00:42:02 +0000419void
420ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
421{
422 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000423
Greg Clayton5160ce52013-03-27 23:08:40 +0000424 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000425
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000426 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000427 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p",
428 static_cast<void*>(src_ast), static_cast<void*>(dst_ast));
429
Sean Callanan99732312011-11-29 00:42:02 +0000430 if (!md)
431 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000432
Sean Callanan99732312011-11-29 00:42:02 +0000433 md->m_minions.erase(src_ast);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000434
Sean Callanan99732312011-11-29 00:42:02 +0000435 for (OriginMap::iterator iter = md->m_origins.begin();
436 iter != md->m_origins.end();
437 )
438 {
439 if (iter->second.ctx == src_ast)
440 md->m_origins.erase(iter++);
441 else
442 ++iter;
443 }
444}
445
Sean Callanan0eed0d42011-12-06 03:41:14 +0000446ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000447{
448 return;
449}
450
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000451void
Sean Callanane55bc8a2013-03-30 02:31:21 +0000452ClangASTImporter::Minion::InitDeportWorkQueues (std::set<clang::NamedDecl *> *decls_to_deport,
453 std::set<clang::NamedDecl *> *decls_already_deported)
454{
455 assert(!m_decls_to_deport); // TODO make debug only
456 assert(!m_decls_already_deported);
457
458 m_decls_to_deport = decls_to_deport;
459 m_decls_already_deported = decls_already_deported;
460}
461
462void
463ClangASTImporter::Minion::ExecuteDeportWorkQueues ()
464{
465 assert(m_decls_to_deport); // TODO make debug only
466 assert(m_decls_already_deported);
467
468 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&getToContext());
469
470 while (!m_decls_to_deport->empty())
471 {
472 NamedDecl *decl = *m_decls_to_deport->begin();
473
474 m_decls_already_deported->insert(decl);
475 m_decls_to_deport->erase(decl);
476
477 DeclOrigin &origin = to_context_md->m_origins[decl];
478
479 assert (origin.ctx == m_source_ctx); // otherwise we should never have added this
480 // because it doesn't need to be deported
481
482 Decl *original_decl = to_context_md->m_origins[decl].decl;
483
484 ClangASTContext::GetCompleteDecl (m_source_ctx, original_decl);
485
486 if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
487 {
488 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
489 if (original_tag_decl->isCompleteDefinition())
490 ImportDefinitionTo(tag_decl, original_tag_decl);
491
492 tag_decl->setHasExternalLexicalStorage(false);
493 tag_decl->setHasExternalVisibleStorage(false);
494 }
495 else if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
496 {
497 interface_decl->setHasExternalLexicalStorage(false);
498 interface_decl->setHasExternalVisibleStorage(false);
499 }
500
501 to_context_md->m_origins.erase(decl);
502 }
503
Ed Masted4612ad2014-04-20 13:17:36 +0000504 m_decls_to_deport = nullptr;
505 m_decls_already_deported = nullptr;
Sean Callanane55bc8a2013-03-30 02:31:21 +0000506}
507
508void
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000509ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
510{
511 ASTImporter::Imported(from, to);
Sean Callanan5b26f272012-02-04 08:49:35 +0000512
513 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
514
515 /*
516 if (to_objc_interface)
517 to_objc_interface->startDefinition();
518
519 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000520
Sean Callanan5b26f272012-02-04 08:49:35 +0000521 if (to_cxx_record)
522 to_cxx_record->startDefinition();
523 */
524
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000525 ImportDefinition(from);
Sean Callanan5b26f272012-02-04 08:49:35 +0000526
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000527 // If we're dealing with an Objective-C class, ensure that the inheritance has
528 // been set up correctly. The ASTImporter may not do this correctly if the
529 // class was originally sourced from symbols.
530
Sean Callanan5b26f272012-02-04 08:49:35 +0000531 if (to_objc_interface)
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000532 {
533 do
534 {
535 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
536
537 if (to_superclass)
538 break; // we're not going to override it if it's set
539
540 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
541
542 if (!from_objc_interface)
543 break;
544
545 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
546
547 if (!from_superclass)
548 break;
549
550 Decl *imported_from_superclass_decl = Import(from_superclass);
551
552 if (!imported_from_superclass_decl)
553 break;
554
555 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
556
557 if (!imported_from_superclass)
558 break;
559
Sean Callanan5b26f272012-02-04 08:49:35 +0000560 if (!to_objc_interface->hasDefinition())
561 to_objc_interface->startDefinition();
562
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000563 to_objc_interface->setSuperClass(imported_from_superclass);
564 }
565 while (0);
566 }
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000567}
568
Greg Clayton57ee3062013-07-11 22:46:58 +0000569clang::Decl *
570ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
Sean Callanancc427fa2011-07-30 02:42:06 +0000571{
Sean Callanan8106d802013-03-08 20:04:57 +0000572 ClangASTMetrics::RegisterClangImport();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000573
Greg Clayton5160ce52013-03-27 23:08:40 +0000574 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000575
Sean Callanan00f43622011-11-18 03:28:09 +0000576 if (log)
577 {
Jim Ingham379397632012-10-27 02:54:13 +0000578 lldb::user_id_t user_id;
579 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
580 if (metadata)
581 user_id = metadata->GetUserID();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000582
Sean Callanan00f43622011-11-18 03:28:09 +0000583 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
584 {
Sean Callanan48e894b2012-05-25 18:12:26 +0000585 std::string name_string;
586 llvm::raw_string_ostream name_stream(name_string);
587 from_named_decl->printName(name_stream);
588 name_stream.flush();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000589
Daniel Malead01b2952012-11-29 21:49:15 +0000590 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000591 from->getDeclKindName(), static_cast<void*>(to),
592 name_string.c_str(), static_cast<void*>(from),
Jim Ingham379397632012-10-27 02:54:13 +0000593 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000594 }
595 else
596 {
Daniel Malead01b2952012-11-29 21:49:15 +0000597 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000598 from->getDeclKindName(), static_cast<void*>(to),
599 static_cast<void*>(from), user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000600 }
601 }
602
Sean Callananb0b87a52011-11-16 22:23:28 +0000603 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
604 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000605
Sean Callananb0b87a52011-11-16 22:23:28 +0000606 if (from_context_md)
607 {
608 OriginMap &origins = from_context_md->m_origins;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000609
Sean Callananb0b87a52011-11-16 22:23:28 +0000610 OriginMap::iterator origin_iter = origins.find(from);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000611
Sean Callananb0b87a52011-11-16 22:23:28 +0000612 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000613 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000614 to_context_md->m_origins[to] = origin_iter->second;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000615
Sean Callanan5bc5a762011-12-20 23:55:47 +0000616 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000617
Sean Callanan5bc5a762011-12-20 23:55:47 +0000618 if (direct_completer.get() != this)
619 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000620
Sean Callanan00f43622011-11-18 03:28:09 +0000621 if (log)
622 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000623 static_cast<void*>(origin_iter->second.decl),
624 static_cast<void*>(origin_iter->second.ctx),
625 static_cast<void*>(&from->getASTContext()),
626 static_cast<void*>(&to->getASTContext()));
Sean Callanan00f43622011-11-18 03:28:09 +0000627 }
628 else
629 {
Sean Callanane55bc8a2013-03-30 02:31:21 +0000630 if (m_decls_to_deport && m_decls_already_deported)
631 {
632 if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to))
633 {
634 NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000635
Sean Callanane55bc8a2013-03-30 02:31:21 +0000636 if (!m_decls_already_deported->count(to_named_decl))
637 m_decls_to_deport->insert(to_named_decl);
638 }
639
640 }
Sean Callanan0eed0d42011-12-06 03:41:14 +0000641 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000642
Sean Callanan00f43622011-11-18 03:28:09 +0000643 if (log)
644 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000645 static_cast<void*>(&from->getASTContext()));
Sean Callanan00f43622011-11-18 03:28:09 +0000646 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000647
Sean Callananb0b87a52011-11-16 22:23:28 +0000648 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
649 {
650 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000651
Sean Callananb0b87a52011-11-16 22:23:28 +0000652 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000653
Sean Callananb0b87a52011-11-16 22:23:28 +0000654 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000655
Sean Callananb0b87a52011-11-16 22:23:28 +0000656 if (namespace_map_iter != namespace_maps.end())
657 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
658 }
659 }
660 else
661 {
662 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000663
Sean Callanan00f43622011-11-18 03:28:09 +0000664 if (log)
665 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000666 static_cast<void*>(from),
667 static_cast<void*>(m_source_ctx),
668 static_cast<void*>(&to->getASTContext()));
Sean Callananb0b87a52011-11-16 22:23:28 +0000669 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000670
Sean Callanancc427fa2011-07-30 02:42:06 +0000671 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
672 {
673 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000674
Sean Callanancc427fa2011-07-30 02:42:06 +0000675 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan3d654b32012-09-24 22:25:51 +0000676 to_tag_decl->setMustBuildLookupTable();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000677
Sean Callanancc427fa2011-07-30 02:42:06 +0000678 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000679 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000680 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000681 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
682 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
683 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanancc427fa2011-07-30 02:42:06 +0000684 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000685
Sean Callananb2269162011-10-21 22:18:07 +0000686 if (isa<NamespaceDecl>(from))
687 {
688 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000689
Sean Callananb2269162011-10-21 22:18:07 +0000690 m_master.BuildNamespaceMap(to_namespace_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000691
Sean Callananb2269162011-10-21 22:18:07 +0000692 to_namespace_decl->setHasExternalVisibleStorage();
693 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000694
Sean Callanan00f43622011-11-18 03:28:09 +0000695 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000696 {
697 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000698
Sean Callanand5c17ed2011-11-15 02:11:17 +0000699 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000700 to_interface_decl->setHasExternalVisibleStorage();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000701
Sean Callanan5b26f272012-02-04 08:49:35 +0000702 /*to_interface_decl->setExternallyCompleted();*/
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000703
Sean Callanand5c17ed2011-11-15 02:11:17 +0000704 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000705 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000706 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
707 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
Sean Callanan5b26f272012-02-04 08:49:35 +0000708 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000709 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000710
Sean Callanancc427fa2011-07-30 02:42:06 +0000711 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000712}
Sean Callanan931a0de2013-10-09 22:33:34 +0000713
714clang::Decl *ClangASTImporter::Minion::GetOriginalDecl (clang::Decl *To)
715{
716 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&To->getASTContext());
717
718 if (!to_context_md)
Ed Masted4612ad2014-04-20 13:17:36 +0000719 return nullptr;
Sean Callanan931a0de2013-10-09 22:33:34 +0000720
721 OriginMap::iterator iter = to_context_md->m_origins.find(To);
722
723 if (iter == to_context_md->m_origins.end())
Ed Masted4612ad2014-04-20 13:17:36 +0000724 return nullptr;
Sean Callanan931a0de2013-10-09 22:33:34 +0000725
726 return const_cast<clang::Decl*>(iter->second.decl);
727}