blob: f6c8880bbcb4e9b4c6350ce0454cee2cdc9a4cd4 [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 {
Jason Molenda900dbdc2014-10-15 23:27:12 +000089 lldb::user_id_t user_id = LLDB_INVALID_UID;
Jim Ingham379397632012-10-27 02:54:13 +000090 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);
Greg Clayton6292b30b2015-04-13 18:32:54 +0000277
278 if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
279 RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
280
Sean Callanan12014a02011-12-08 23:45:45 +0000281 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000282}
283
Sean Callanan6b200d02013-03-21 22:15:41 +0000284bool
285ClangASTImporter::RequireCompleteType (clang::QualType type)
286{
287 if (type.isNull())
288 return false;
289
290 if (const TagType *tag_type = type->getAs<TagType>())
291 {
Stephane Sezer9901e9c2015-04-08 21:52:45 +0000292 TagDecl *tag_decl = tag_type->getDecl();
293
294 if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
295 return true;
296
297 return CompleteTagDecl(tag_decl);
Sean Callanan6b200d02013-03-21 22:15:41 +0000298 }
299 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>())
300 {
301 if (ObjCInterfaceDecl *objc_interface_decl = objc_object_type->getInterface())
302 return CompleteObjCInterfaceDecl(objc_interface_decl);
303 else
304 return false;
305 }
306 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
307 {
308 return RequireCompleteType(array_type->getElementType());
309 }
310 if (const AtomicType *atomic_type = type->getAs<AtomicType>())
311 {
312 return RequireCompleteType(atomic_type->getPointeeType());
313 }
314
315 return true;
316}
317
Jim Ingham379397632012-10-27 02:54:13 +0000318ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +0000319ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
320{
321 DeclOrigin decl_origin = GetDeclOrigin(decl);
322
323 if (decl_origin.Valid())
Greg Claytond0029442013-03-27 01:48:02 +0000324 return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
Sean Callanan60217122012-04-13 00:10:03 +0000325 else
Greg Claytond0029442013-03-27 01:48:02 +0000326 return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
Sean Callanan60217122012-04-13 00:10:03 +0000327}
328
Sean Callananf487bd82011-11-16 21:40:57 +0000329ClangASTImporter::DeclOrigin
330ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
331{
332 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
333
334 OriginMap &origins = context_md->m_origins;
335
336 OriginMap::iterator iter = origins.find(decl);
337
338 if (iter != origins.end())
339 return iter->second;
340 else
341 return DeclOrigin();
342}
343
Sean Callanan2cb5e522012-09-20 23:21:16 +0000344void
345ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
346{
347 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
348
349 OriginMap &origins = context_md->m_origins;
350
351 OriginMap::iterator iter = origins.find(decl);
352
353 if (iter != origins.end())
354 {
355 iter->second.decl = original_decl;
356 iter->second.ctx = &original_decl->getASTContext();
357 }
358 else
359 {
360 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
361 }
362}
363
364void
Sean Callanan503aa522011-10-12 00:12:34 +0000365ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
366 NamespaceMapSP &namespace_map)
367{
Sean Callananf487bd82011-11-16 21:40:57 +0000368 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
369
370 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000371}
372
373ClangASTImporter::NamespaceMapSP
374ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
375{
Sean Callananf487bd82011-11-16 21:40:57 +0000376 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
377
378 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000379
Sean Callananf487bd82011-11-16 21:40:57 +0000380 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
381
382 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000383 return iter->second;
384 else
385 return NamespaceMapSP();
386}
387
Sean Callananb2269162011-10-21 22:18:07 +0000388void
389ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
390{
Jim Ingham28eb5712012-10-12 17:34:26 +0000391 assert (decl);
Sean Callananf487bd82011-11-16 21:40:57 +0000392 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
393
Sean Callananb2269162011-10-21 22:18:07 +0000394 const DeclContext *parent_context = decl->getDeclContext();
395 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
396 NamespaceMapSP parent_map;
397
398 if (parent_namespace)
399 parent_map = GetNamespaceMap(parent_namespace);
400
401 NamespaceMapSP new_map;
402
403 new_map.reset(new NamespaceMap);
404
Sean Callananf487bd82011-11-16 21:40:57 +0000405 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000406 {
407 std::string namespace_string = decl->getDeclName().getAsString();
408
Sean Callananf487bd82011-11-16 21:40:57 +0000409 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000410 }
411
Sean Callanan0eed0d42011-12-06 03:41:14 +0000412 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000413}
414
Sean Callanan686b2312011-11-16 18:20:47 +0000415void
Sean Callanan99732312011-11-29 00:42:02 +0000416ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000417{
Greg Clayton5160ce52013-03-27 23:08:40 +0000418 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000419
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000420 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000421 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p",
422 static_cast<void*>(dst_ast));
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000423
Sean Callananf487bd82011-11-16 21:40:57 +0000424 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000425}
426
Sean Callanan99732312011-11-29 00:42:02 +0000427void
428ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
429{
430 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000431
Greg Clayton5160ce52013-03-27 23:08:40 +0000432 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000433
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000434 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000435 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p",
436 static_cast<void*>(src_ast), static_cast<void*>(dst_ast));
437
Sean Callanan99732312011-11-29 00:42:02 +0000438 if (!md)
439 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000440
Sean Callanan99732312011-11-29 00:42:02 +0000441 md->m_minions.erase(src_ast);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000442
Sean Callanan99732312011-11-29 00:42:02 +0000443 for (OriginMap::iterator iter = md->m_origins.begin();
444 iter != md->m_origins.end();
445 )
446 {
447 if (iter->second.ctx == src_ast)
448 md->m_origins.erase(iter++);
449 else
450 ++iter;
451 }
452}
453
Sean Callanan0eed0d42011-12-06 03:41:14 +0000454ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000455{
456 return;
457}
458
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000459void
Sean Callanane55bc8a2013-03-30 02:31:21 +0000460ClangASTImporter::Minion::InitDeportWorkQueues (std::set<clang::NamedDecl *> *decls_to_deport,
461 std::set<clang::NamedDecl *> *decls_already_deported)
462{
463 assert(!m_decls_to_deport); // TODO make debug only
464 assert(!m_decls_already_deported);
465
466 m_decls_to_deport = decls_to_deport;
467 m_decls_already_deported = decls_already_deported;
468}
469
470void
471ClangASTImporter::Minion::ExecuteDeportWorkQueues ()
472{
473 assert(m_decls_to_deport); // TODO make debug only
474 assert(m_decls_already_deported);
475
476 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&getToContext());
477
478 while (!m_decls_to_deport->empty())
479 {
480 NamedDecl *decl = *m_decls_to_deport->begin();
481
482 m_decls_already_deported->insert(decl);
483 m_decls_to_deport->erase(decl);
484
485 DeclOrigin &origin = to_context_md->m_origins[decl];
486
487 assert (origin.ctx == m_source_ctx); // otherwise we should never have added this
488 // because it doesn't need to be deported
489
490 Decl *original_decl = to_context_md->m_origins[decl].decl;
491
492 ClangASTContext::GetCompleteDecl (m_source_ctx, original_decl);
493
494 if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
495 {
496 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
497 if (original_tag_decl->isCompleteDefinition())
498 ImportDefinitionTo(tag_decl, original_tag_decl);
499
500 tag_decl->setHasExternalLexicalStorage(false);
501 tag_decl->setHasExternalVisibleStorage(false);
502 }
503 else if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
504 {
505 interface_decl->setHasExternalLexicalStorage(false);
506 interface_decl->setHasExternalVisibleStorage(false);
507 }
508
509 to_context_md->m_origins.erase(decl);
510 }
511
Ed Masted4612ad2014-04-20 13:17:36 +0000512 m_decls_to_deport = nullptr;
513 m_decls_already_deported = nullptr;
Sean Callanane55bc8a2013-03-30 02:31:21 +0000514}
515
516void
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000517ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
518{
519 ASTImporter::Imported(from, to);
Sean Callanan5b26f272012-02-04 08:49:35 +0000520
521 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
522
523 /*
524 if (to_objc_interface)
525 to_objc_interface->startDefinition();
526
527 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000528
Sean Callanan5b26f272012-02-04 08:49:35 +0000529 if (to_cxx_record)
530 to_cxx_record->startDefinition();
531 */
532
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000533 ImportDefinition(from);
Sean Callanan5b26f272012-02-04 08:49:35 +0000534
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000535 // If we're dealing with an Objective-C class, ensure that the inheritance has
536 // been set up correctly. The ASTImporter may not do this correctly if the
537 // class was originally sourced from symbols.
538
Sean Callanan5b26f272012-02-04 08:49:35 +0000539 if (to_objc_interface)
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000540 {
541 do
542 {
543 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
544
545 if (to_superclass)
546 break; // we're not going to override it if it's set
547
548 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
549
550 if (!from_objc_interface)
551 break;
552
553 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
554
555 if (!from_superclass)
556 break;
557
558 Decl *imported_from_superclass_decl = Import(from_superclass);
559
560 if (!imported_from_superclass_decl)
561 break;
562
563 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
564
565 if (!imported_from_superclass)
566 break;
567
Sean Callanan5b26f272012-02-04 08:49:35 +0000568 if (!to_objc_interface->hasDefinition())
569 to_objc_interface->startDefinition();
570
Pavel Labath67add942015-07-07 10:11:16 +0000571 to_objc_interface->setSuperClass(
572 m_source_ctx->getTrivialTypeSourceInfo(m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000573 }
574 while (0);
575 }
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000576}
577
Greg Clayton57ee3062013-07-11 22:46:58 +0000578clang::Decl *
579ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
Sean Callanancc427fa2011-07-30 02:42:06 +0000580{
Sean Callanan8106d802013-03-08 20:04:57 +0000581 ClangASTMetrics::RegisterClangImport();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000582
Greg Clayton5160ce52013-03-27 23:08:40 +0000583 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000584
Sean Callanan608fb392014-08-01 22:42:38 +0000585 lldb::user_id_t user_id = LLDB_INVALID_UID;
586 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
587 if (metadata)
588 user_id = metadata->GetUserID();
589
Sean Callanan00f43622011-11-18 03:28:09 +0000590 if (log)
591 {
592 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
593 {
Sean Callanan48e894b2012-05-25 18:12:26 +0000594 std::string name_string;
595 llvm::raw_string_ostream name_stream(name_string);
596 from_named_decl->printName(name_stream);
597 name_stream.flush();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000598
Daniel Malead01b2952012-11-29 21:49:15 +0000599 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000600 from->getDeclKindName(), static_cast<void*>(to),
601 name_string.c_str(), static_cast<void*>(from),
Jim Ingham379397632012-10-27 02:54:13 +0000602 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000603 }
604 else
605 {
Daniel Malead01b2952012-11-29 21:49:15 +0000606 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000607 from->getDeclKindName(), static_cast<void*>(to),
608 static_cast<void*>(from), user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000609 }
610 }
611
Sean Callananb0b87a52011-11-16 22:23:28 +0000612 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
613 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000614
Sean Callananb0b87a52011-11-16 22:23:28 +0000615 if (from_context_md)
616 {
617 OriginMap &origins = from_context_md->m_origins;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000618
Sean Callananb0b87a52011-11-16 22:23:28 +0000619 OriginMap::iterator origin_iter = origins.find(from);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000620
Sean Callananb0b87a52011-11-16 22:23:28 +0000621 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000622 {
Sean Callanan608fb392014-08-01 22:42:38 +0000623 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
624 user_id != LLDB_INVALID_UID)
625 {
626 to_context_md->m_origins[to] = origin_iter->second;
627 }
628
Sean Callanan5bc5a762011-12-20 23:55:47 +0000629 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000630
Sean Callanan5bc5a762011-12-20 23:55:47 +0000631 if (direct_completer.get() != this)
632 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000633
Sean Callanan00f43622011-11-18 03:28:09 +0000634 if (log)
635 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000636 static_cast<void*>(origin_iter->second.decl),
637 static_cast<void*>(origin_iter->second.ctx),
638 static_cast<void*>(&from->getASTContext()),
639 static_cast<void*>(&to->getASTContext()));
Sean Callanan00f43622011-11-18 03:28:09 +0000640 }
641 else
642 {
Sean Callanane55bc8a2013-03-30 02:31:21 +0000643 if (m_decls_to_deport && m_decls_already_deported)
644 {
645 if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to))
646 {
647 NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000648
Sean Callanane55bc8a2013-03-30 02:31:21 +0000649 if (!m_decls_already_deported->count(to_named_decl))
650 m_decls_to_deport->insert(to_named_decl);
651 }
Sean Callanane55bc8a2013-03-30 02:31:21 +0000652 }
Sean Callanan608fb392014-08-01 22:42:38 +0000653
654 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
655 user_id != LLDB_INVALID_UID)
656 {
657 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
658 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000659
Sean Callanan00f43622011-11-18 03:28:09 +0000660 if (log)
661 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000662 static_cast<void*>(&from->getASTContext()));
Sean Callanan00f43622011-11-18 03:28:09 +0000663 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000664
Sean Callananb0b87a52011-11-16 22:23:28 +0000665 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
666 {
667 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000668
Sean Callananb0b87a52011-11-16 22:23:28 +0000669 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000670
Sean Callananb0b87a52011-11-16 22:23:28 +0000671 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000672
Sean Callananb0b87a52011-11-16 22:23:28 +0000673 if (namespace_map_iter != namespace_maps.end())
674 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
675 }
676 }
677 else
678 {
679 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000680
Sean Callanan00f43622011-11-18 03:28:09 +0000681 if (log)
682 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000683 static_cast<void*>(from),
684 static_cast<void*>(m_source_ctx),
685 static_cast<void*>(&to->getASTContext()));
Sean Callananb0b87a52011-11-16 22:23:28 +0000686 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000687
Sean Callanancc427fa2011-07-30 02:42:06 +0000688 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
689 {
690 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000691
Sean Callanancc427fa2011-07-30 02:42:06 +0000692 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan3d654b32012-09-24 22:25:51 +0000693 to_tag_decl->setMustBuildLookupTable();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000694
Sean Callanancc427fa2011-07-30 02:42:06 +0000695 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000696 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000697 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000698 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
699 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
700 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanancc427fa2011-07-30 02:42:06 +0000701 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000702
Sean Callananb2269162011-10-21 22:18:07 +0000703 if (isa<NamespaceDecl>(from))
704 {
705 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000706
Sean Callananb2269162011-10-21 22:18:07 +0000707 m_master.BuildNamespaceMap(to_namespace_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000708
Sean Callananb2269162011-10-21 22:18:07 +0000709 to_namespace_decl->setHasExternalVisibleStorage();
710 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000711
Sean Callanan00f43622011-11-18 03:28:09 +0000712 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000713 {
714 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000715
Sean Callanand5c17ed2011-11-15 02:11:17 +0000716 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000717 to_interface_decl->setHasExternalVisibleStorage();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000718
Sean Callanan5b26f272012-02-04 08:49:35 +0000719 /*to_interface_decl->setExternallyCompleted();*/
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000720
Sean Callanand5c17ed2011-11-15 02:11:17 +0000721 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000722 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000723 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
724 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
Sean Callanan5b26f272012-02-04 08:49:35 +0000725 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000726 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000727
Sean Callanancc427fa2011-07-30 02:42:06 +0000728 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000729}
Sean Callanan931a0de2013-10-09 22:33:34 +0000730
731clang::Decl *ClangASTImporter::Minion::GetOriginalDecl (clang::Decl *To)
732{
733 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&To->getASTContext());
734
735 if (!to_context_md)
Ed Masted4612ad2014-04-20 13:17:36 +0000736 return nullptr;
Sean Callanan931a0de2013-10-09 22:33:34 +0000737
738 OriginMap::iterator iter = to_context_md->m_origins.find(To);
739
740 if (iter == to_context_md->m_origins.end())
Ed Masted4612ad2014-04-20 13:17:36 +0000741 return nullptr;
Sean Callanan931a0de2013-10-09 22:33:34 +0000742
743 return const_cast<clang::Decl*>(iter->second.decl);
744}