blob: 6a3ae65822cefd0bfdb3ae7793eb95adcfb8db9b [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 Callanan83b8ad02015-07-08 18:03:41 +000019#include "lldb/Utility/LLDBAssert.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
Bruce Mitchener23a3b0e2015-09-22 17:04:24 +000062lldb::opaque_compiler_type_t
Sean Callanan80f78672011-11-16 19:07:39 +000063ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
64 clang::ASTContext *src_ast,
Bruce Mitchener23a3b0e2015-09-22 17:04:24 +000065 lldb::opaque_compiler_type_t type)
Sean Callanan80f78672011-11-16 19:07:39 +000066{
67 return CopyType (dst_ast, src_ast, QualType::getFromOpaquePtr(type)).getAsOpaquePtr();
68}
69
Greg Claytone6b36cd2015-12-08 01:02:08 +000070CompilerType
71ClangASTImporter::CopyType (ClangASTContext &dst_ast,
72 const CompilerType &src_type)
73{
74 clang::ASTContext *dst_clang_ast = dst_ast.getASTContext();
75 if (dst_clang_ast)
76 {
77 ClangASTContext *src_ast = llvm::dyn_cast_or_null<ClangASTContext>(src_type.GetTypeSystem());
78 if (src_ast)
79 {
80 clang::ASTContext *src_clang_ast = src_ast->getASTContext();
81 if (src_clang_ast)
82 {
83 lldb::opaque_compiler_type_t dst_clang_type = CopyType(dst_clang_ast,
84 src_clang_ast,
85 src_type.GetOpaqueQualType());
86
87 if (dst_clang_type)
88 return CompilerType(&dst_ast, dst_clang_type);
89 }
90 }
91 }
92 return CompilerType();
93}
94
Greg Claytona2721472011-06-25 00:44:06 +000095clang::Decl *
Sean Callanan686b2312011-11-16 18:20:47 +000096ClangASTImporter::CopyDecl (clang::ASTContext *dst_ast,
97 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000098 clang::Decl *decl)
99{
Greg Claytondd0649b2011-07-06 18:55:08 +0000100 MinionSP minion_sp;
Greg Claytona2721472011-06-25 00:44:06 +0000101
Sean Callanan686b2312011-11-16 18:20:47 +0000102 minion_sp = GetMinion(dst_ast, src_ast);
Greg Claytona2721472011-06-25 00:44:06 +0000103
Greg Claytondd0649b2011-07-06 18:55:08 +0000104 if (minion_sp)
Sean Callananbfb237bc2011-11-04 22:46:46 +0000105 {
106 clang::Decl *result = minion_sp->Import(decl);
107
108 if (!result)
109 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000110 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananbfb237bc2011-11-04 22:46:46 +0000111
Sean Callanand5145b32011-11-05 00:08:12 +0000112 if (log)
113 {
Jason Molenda900dbdc2014-10-15 23:27:12 +0000114 lldb::user_id_t user_id = LLDB_INVALID_UID;
Jim Ingham379397632012-10-27 02:54:13 +0000115 ClangASTMetadata *metadata = GetDeclMetadata(decl);
116 if (metadata)
117 user_id = metadata->GetUserID();
118
Sean Callanand5145b32011-11-05 00:08:12 +0000119 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
Daniel Malead01b2952012-11-29 21:49:15 +0000120 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s', metadata 0x%" PRIx64,
Sean Callanand9804fb2012-04-17 22:30:04 +0000121 decl->getDeclKindName(),
122 named_decl->getNameAsString().c_str(),
Jim Ingham379397632012-10-27 02:54:13 +0000123 user_id);
Sean Callanand5145b32011-11-05 00:08:12 +0000124 else
Daniel Malead01b2952012-11-29 21:49:15 +0000125 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s, metadata 0x%" PRIx64,
Sean Callanand9804fb2012-04-17 22:30:04 +0000126 decl->getDeclKindName(),
Jim Ingham379397632012-10-27 02:54:13 +0000127 user_id);
Sean Callanand5145b32011-11-05 00:08:12 +0000128 }
Sean Callananbfb237bc2011-11-04 22:46:46 +0000129 }
130
131 return result;
132 }
Sean Callanancc427fa2011-07-30 02:42:06 +0000133
Ed Masted4612ad2014-04-20 13:17:36 +0000134 return nullptr;
Greg Claytona2721472011-06-25 00:44:06 +0000135}
136
Sean Callanan83b8ad02015-07-08 18:03:41 +0000137class DeclContextOverride
138{
139private:
140 struct Backup
141 {
142 clang::DeclContext *decl_context;
143 clang::DeclContext *lexical_decl_context;
144 };
145
146 std::map<clang::Decl *, Backup> m_backups;
147
148 void OverrideOne(clang::Decl *decl)
149 {
150 if (m_backups.find(decl) != m_backups.end())
151 {
152 return;
153 }
154
155 m_backups[decl] = { decl->getDeclContext(), decl->getLexicalDeclContext() };
156
157 decl->setDeclContext(decl->getASTContext().getTranslationUnitDecl());
158 decl->setLexicalDeclContext(decl->getASTContext().getTranslationUnitDecl());
159 }
160
161 bool ChainPassesThrough(clang::Decl *decl,
162 clang::DeclContext *base,
163 clang::DeclContext *(clang::Decl::*contextFromDecl)(),
164 clang::DeclContext *(clang::DeclContext::*contextFromContext)())
165 {
166 for (DeclContext *decl_ctx = (decl->*contextFromDecl)();
167 decl_ctx;
168 decl_ctx = (decl_ctx->*contextFromContext)())
169 {
170 if (decl_ctx == base)
171 {
172 return true;
173 }
174 }
175
176 return false;
177 }
178
179 clang::Decl *GetEscapedChild(clang::Decl *decl, clang::DeclContext *base = nullptr)
180 {
181 if (base)
182 {
183 // decl's DeclContext chains must pass through base.
184
185 if (!ChainPassesThrough(decl, base, &clang::Decl::getDeclContext, &clang::DeclContext::getParent) ||
186 !ChainPassesThrough(decl, base, &clang::Decl::getLexicalDeclContext, &clang::DeclContext::getLexicalParent))
187 {
188 return decl;
189 }
190 }
191 else
192 {
193 base = clang::dyn_cast<clang::DeclContext>(decl);
194
195 if (!base)
196 {
197 return nullptr;
198 }
199 }
200
201 if (clang::DeclContext *context = clang::dyn_cast<clang::DeclContext>(decl))
202 {
203 for (clang::Decl *decl : context->decls())
204 {
205 if (clang::Decl *escaped_child = GetEscapedChild(decl))
206 {
207 return escaped_child;
208 }
209 }
210 }
211
212 return nullptr;
213 }
214
215 void Override(clang::Decl *decl)
216 {
217 if (clang::Decl *escaped_child = GetEscapedChild(decl))
218 {
219 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
220
221 if (log)
222 log->Printf(" [ClangASTImporter] DeclContextOverride couldn't override (%sDecl*)%p - its child (%sDecl*)%p escapes",
223 decl->getDeclKindName(), static_cast<void*>(decl),
224 escaped_child->getDeclKindName(), static_cast<void*>(escaped_child));
225 lldbassert(0 && "Couldn't override!");
226 }
227
228 OverrideOne(decl);
229 }
230
231public:
232 DeclContextOverride()
233 {
234 }
235
236 void OverrideAllDeclsFromContainingFunction(clang::Decl *decl)
237 {
238 for (DeclContext *decl_context = decl->getLexicalDeclContext();
239 decl_context;
240 decl_context = decl_context->getLexicalParent())
241 {
242 DeclContext *redecl_context = decl_context->getRedeclContext();
243
244 if (llvm::isa<FunctionDecl>(redecl_context) &&
245 llvm::isa<TranslationUnitDecl>(redecl_context->getLexicalParent()))
246 {
247 for (clang::Decl *child_decl : decl_context->decls())
248 {
249 Override(child_decl);
250 }
251 }
252 }
253 }
254
255 ~DeclContextOverride()
256 {
257 for (const std::pair<clang::Decl *, Backup> &backup : m_backups)
258 {
259 backup.first->setDeclContext(backup.second.decl_context);
260 backup.first->setLexicalDeclContext(backup.second.lexical_decl_context);
261 }
262 }
263};
264
Bruce Mitchener23a3b0e2015-09-22 17:04:24 +0000265lldb::opaque_compiler_type_t
Sean Callananbb120042011-12-16 21:06:35 +0000266ClangASTImporter::DeportType (clang::ASTContext *dst_ctx,
267 clang::ASTContext *src_ctx,
Bruce Mitchener23a3b0e2015-09-22 17:04:24 +0000268 lldb::opaque_compiler_type_t type)
Sean Callanan4a2a71f2015-09-02 16:39:23 +0000269{
270 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
271
272 if (log)
273 log->Printf(" [ClangASTImporter] DeportType called on (%sType*)0x%llx from (ASTContext*)%p to (ASTContext*)%p",
274 QualType::getFromOpaquePtr(type)->getTypeClassName(), (unsigned long long)type,
275 static_cast<void*>(src_ctx),
276 static_cast<void*>(dst_ctx));
277
Sean Callanane55bc8a2013-03-30 02:31:21 +0000278 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
279
280 if (!minion_sp)
Ed Masted4612ad2014-04-20 13:17:36 +0000281 return nullptr;
Sean Callanane55bc8a2013-03-30 02:31:21 +0000282
283 std::set<NamedDecl *> decls_to_deport;
284 std::set<NamedDecl *> decls_already_deported;
285
Sean Callanan83b8ad02015-07-08 18:03:41 +0000286 DeclContextOverride decl_context_override;
287
288 if (const clang::TagType *tag_type = clang::QualType::getFromOpaquePtr(type)->getAs<TagType>())
289 {
290 decl_context_override.OverrideAllDeclsFromContainingFunction(tag_type->getDecl());
291 }
292
Sean Callanane55bc8a2013-03-30 02:31:21 +0000293 minion_sp->InitDeportWorkQueues(&decls_to_deport,
294 &decls_already_deported);
295
Bruce Mitchener23a3b0e2015-09-22 17:04:24 +0000296 lldb::opaque_compiler_type_t result = CopyType(dst_ctx, src_ctx, type);
Sean Callananbb120042011-12-16 21:06:35 +0000297
Sean Callanane55bc8a2013-03-30 02:31:21 +0000298 minion_sp->ExecuteDeportWorkQueues();
299
Sean Callananbb120042011-12-16 21:06:35 +0000300 if (!result)
Ed Masted4612ad2014-04-20 13:17:36 +0000301 return nullptr;
Sean Callananbb120042011-12-16 21:06:35 +0000302
Sean Callananbb120042011-12-16 21:06:35 +0000303 return result;
Sean Callanane55bc8a2013-03-30 02:31:21 +0000304
Sean Callananbb120042011-12-16 21:06:35 +0000305}
306
Sean Callanan0eed0d42011-12-06 03:41:14 +0000307clang::Decl *
308ClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx,
309 clang::ASTContext *src_ctx,
310 clang::Decl *decl)
311{
Greg Clayton5160ce52013-03-27 23:08:40 +0000312 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000313
Sean Callanan933ca2e2013-02-28 03:12:58 +0000314 if (log)
Sean Callanan4a2a71f2015-09-02 16:39:23 +0000315 log->Printf(" [ClangASTImporter] DeportDecl called on (%sDecl*)%p from (ASTContext*)%p to (ASTContext*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000316 decl->getDeclKindName(), static_cast<void*>(decl),
317 static_cast<void*>(src_ctx),
318 static_cast<void*>(dst_ctx));
319
Sean Callanane55bc8a2013-03-30 02:31:21 +0000320 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000321
Sean Callanane55bc8a2013-03-30 02:31:21 +0000322 if (!minion_sp)
Ed Masted4612ad2014-04-20 13:17:36 +0000323 return nullptr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000324
Sean Callanane55bc8a2013-03-30 02:31:21 +0000325 std::set<NamedDecl *> decls_to_deport;
326 std::set<NamedDecl *> decls_already_deported;
Sean Callanan83b8ad02015-07-08 18:03:41 +0000327
328 DeclContextOverride decl_context_override;
329
330 decl_context_override.OverrideAllDeclsFromContainingFunction(decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000331
Sean Callanane55bc8a2013-03-30 02:31:21 +0000332 minion_sp->InitDeportWorkQueues(&decls_to_deport,
333 &decls_already_deported);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000334
Sean Callanan0eed0d42011-12-06 03:41:14 +0000335 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
Sean Callanane55bc8a2013-03-30 02:31:21 +0000336
337 minion_sp->ExecuteDeportWorkQueues();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000338
Sean Callanan0eed0d42011-12-06 03:41:14 +0000339 if (!result)
Ed Masted4612ad2014-04-20 13:17:36 +0000340 return nullptr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000341
Sean Callanan933ca2e2013-02-28 03:12:58 +0000342 if (log)
343 log->Printf(" [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000344 decl->getDeclKindName(), static_cast<void*>(decl),
345 result->getDeclKindName(), static_cast<void*>(result));
346
Sean Callanan0eed0d42011-12-06 03:41:14 +0000347 return result;
348}
349
Sean Callanan5b26f272012-02-04 08:49:35 +0000350void
351ClangASTImporter::CompleteDecl (clang::Decl *decl)
352{
Greg Clayton5160ce52013-03-27 23:08:40 +0000353 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5b26f272012-02-04 08:49:35 +0000354
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000355 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +0000356 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000357 decl->getDeclKindName(), static_cast<void*>(decl));
358
Sean Callanan5b26f272012-02-04 08:49:35 +0000359 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
360 {
361 if (!interface_decl->getDefinition())
362 {
363 interface_decl->startDefinition();
364 CompleteObjCInterfaceDecl(interface_decl);
365 }
366 }
Greg Clayton23f59502012-07-17 03:23:13 +0000367 else if (ObjCProtocolDecl *protocol_decl = dyn_cast<ObjCProtocolDecl>(decl))
Sean Callanan5b26f272012-02-04 08:49:35 +0000368 {
369 if (!protocol_decl->getDefinition())
370 protocol_decl->startDefinition();
371 }
372 else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
373 {
374 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined())
375 {
376 tag_decl->startDefinition();
377 CompleteTagDecl(tag_decl);
378 tag_decl->setCompleteDefinition(true);
379 }
380 }
Greg Clayton219cf312012-03-30 00:51:13 +0000381 else
382 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000383 assert (0 && "CompleteDecl called on a Decl that can't be completed");
384 }
385}
386
Sean Callanan12014a02011-12-08 23:45:45 +0000387bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000388ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Sean Callanan8106d802013-03-08 20:04:57 +0000389{
390 ClangASTMetrics::RegisterDeclCompletion();
391
Sean Callanancc427fa2011-07-30 02:42:06 +0000392 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +0000393
Sean Callanancc427fa2011-07-30 02:42:06 +0000394 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000395 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000396
Sean Callanancc427fa2011-07-30 02:42:06 +0000397 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000398 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000399
Sean Callanan686b2312011-11-16 18:20:47 +0000400 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
Greg Claytona2721472011-06-25 00:44:06 +0000401
Greg Claytondd0649b2011-07-06 18:55:08 +0000402 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000403 minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
404
Sean Callanan12014a02011-12-08 23:45:45 +0000405 return true;
Greg Claytona2721472011-06-25 00:44:06 +0000406}
Sean Callanancc427fa2011-07-30 02:42:06 +0000407
Sean Callanan12014a02011-12-08 23:45:45 +0000408bool
409ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
410{
Sean Callanan8106d802013-03-08 20:04:57 +0000411 ClangASTMetrics::RegisterDeclCompletion();
412
Sean Callanan12014a02011-12-08 23:45:45 +0000413 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
414
415 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
416 return false;
417
418 MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
419
420 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000421 minion_sp->ImportDefinitionTo(decl, origin_decl);
422
Sean Callanan12014a02011-12-08 23:45:45 +0000423 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
424
425 OriginMap &origins = context_md->m_origins;
426
427 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
428
429 return true;
430}
431
432bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000433ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
434{
Sean Callanan8106d802013-03-08 20:04:57 +0000435 ClangASTMetrics::RegisterDeclCompletion();
Sean Callanancc427fa2011-07-30 02:42:06 +0000436
Sean Callanancc427fa2011-07-30 02:42:06 +0000437 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
438
439 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000440 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000441
442 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000443 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000444
Sean Callanan686b2312011-11-16 18:20:47 +0000445 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000446
447 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000448 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
Greg Clayton6292b30b2015-04-13 18:32:54 +0000449
450 if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
451 RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
452
Sean Callanan12014a02011-12-08 23:45:45 +0000453 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000454}
455
Sean Callanan6b200d02013-03-21 22:15:41 +0000456bool
Greg Claytone6b36cd2015-12-08 01:02:08 +0000457ClangASTImporter::CompleteAndFetchChildren (clang::QualType type)
458{
459 if (!RequireCompleteType(type))
460 return false;
461
462 if (const TagType *tag_type = type->getAs<TagType>())
463 {
464 TagDecl *tag_decl = tag_type->getDecl();
465
466 DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
467
468 if (!decl_origin.Valid())
469 return false;
470
471 MinionSP minion_sp (GetMinion(&tag_decl->getASTContext(), decl_origin.ctx));
472
473 TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
474
475 for (Decl *origin_child_decl : origin_tag_decl->decls())
476 {
477 minion_sp->Import(origin_child_decl);
478 }
479
480 if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl))
481 {
482 record_decl->setHasLoadedFieldsFromExternalStorage(true);
483 }
484
485 return true;
486 }
487
488 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>())
489 {
490 if (ObjCInterfaceDecl *objc_interface_decl = objc_object_type->getInterface())
491 {
492 DeclOrigin decl_origin = GetDeclOrigin(objc_interface_decl);
493
494 if (!decl_origin.Valid())
495 return false;
496
497 MinionSP minion_sp (GetMinion(&objc_interface_decl->getASTContext(), decl_origin.ctx));
498
499 ObjCInterfaceDecl *origin_interface_decl = llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
500
501 for (Decl *origin_child_decl : origin_interface_decl->decls())
502 {
503 minion_sp->Import(origin_child_decl);
504 }
505
506 return true;
507 }
508 else
509 {
510 return false;
511 }
512 }
513
514 return true;
515}
516
517
518bool
Sean Callanan6b200d02013-03-21 22:15:41 +0000519ClangASTImporter::RequireCompleteType (clang::QualType type)
520{
521 if (type.isNull())
522 return false;
523
524 if (const TagType *tag_type = type->getAs<TagType>())
525 {
Stephane Sezer9901e9c2015-04-08 21:52:45 +0000526 TagDecl *tag_decl = tag_type->getDecl();
527
528 if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
529 return true;
530
531 return CompleteTagDecl(tag_decl);
Sean Callanan6b200d02013-03-21 22:15:41 +0000532 }
533 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>())
534 {
535 if (ObjCInterfaceDecl *objc_interface_decl = objc_object_type->getInterface())
536 return CompleteObjCInterfaceDecl(objc_interface_decl);
537 else
538 return false;
539 }
540 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
541 {
542 return RequireCompleteType(array_type->getElementType());
543 }
544 if (const AtomicType *atomic_type = type->getAs<AtomicType>())
545 {
546 return RequireCompleteType(atomic_type->getPointeeType());
547 }
548
549 return true;
550}
551
Jim Ingham379397632012-10-27 02:54:13 +0000552ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +0000553ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
554{
555 DeclOrigin decl_origin = GetDeclOrigin(decl);
556
557 if (decl_origin.Valid())
Greg Claytond0029442013-03-27 01:48:02 +0000558 return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
Sean Callanan60217122012-04-13 00:10:03 +0000559 else
Greg Claytond0029442013-03-27 01:48:02 +0000560 return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
Sean Callanan60217122012-04-13 00:10:03 +0000561}
562
Sean Callananf487bd82011-11-16 21:40:57 +0000563ClangASTImporter::DeclOrigin
564ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
565{
566 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
567
568 OriginMap &origins = context_md->m_origins;
569
570 OriginMap::iterator iter = origins.find(decl);
571
572 if (iter != origins.end())
573 return iter->second;
574 else
575 return DeclOrigin();
576}
577
Sean Callanan2cb5e522012-09-20 23:21:16 +0000578void
579ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
580{
581 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
582
583 OriginMap &origins = context_md->m_origins;
584
585 OriginMap::iterator iter = origins.find(decl);
586
587 if (iter != origins.end())
588 {
589 iter->second.decl = original_decl;
590 iter->second.ctx = &original_decl->getASTContext();
591 }
592 else
593 {
594 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
595 }
596}
597
598void
Sean Callanan503aa522011-10-12 00:12:34 +0000599ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
600 NamespaceMapSP &namespace_map)
601{
Sean Callananf487bd82011-11-16 21:40:57 +0000602 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
603
604 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000605}
606
607ClangASTImporter::NamespaceMapSP
608ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
609{
Sean Callananf487bd82011-11-16 21:40:57 +0000610 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
611
612 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000613
Sean Callananf487bd82011-11-16 21:40:57 +0000614 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
615
616 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000617 return iter->second;
618 else
619 return NamespaceMapSP();
620}
621
Sean Callananb2269162011-10-21 22:18:07 +0000622void
623ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
624{
Jim Ingham28eb5712012-10-12 17:34:26 +0000625 assert (decl);
Sean Callananf487bd82011-11-16 21:40:57 +0000626 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
627
Sean Callananb2269162011-10-21 22:18:07 +0000628 const DeclContext *parent_context = decl->getDeclContext();
629 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
630 NamespaceMapSP parent_map;
631
632 if (parent_namespace)
633 parent_map = GetNamespaceMap(parent_namespace);
634
635 NamespaceMapSP new_map;
636
637 new_map.reset(new NamespaceMap);
638
Sean Callananf487bd82011-11-16 21:40:57 +0000639 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000640 {
641 std::string namespace_string = decl->getDeclName().getAsString();
642
Sean Callananf487bd82011-11-16 21:40:57 +0000643 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000644 }
645
Sean Callanan0eed0d42011-12-06 03:41:14 +0000646 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000647}
648
Sean Callanan686b2312011-11-16 18:20:47 +0000649void
Sean Callanan99732312011-11-29 00:42:02 +0000650ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000651{
Greg Clayton5160ce52013-03-27 23:08:40 +0000652 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000653
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000654 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000655 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p",
656 static_cast<void*>(dst_ast));
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000657
Sean Callananf487bd82011-11-16 21:40:57 +0000658 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000659}
660
Sean Callanan99732312011-11-29 00:42:02 +0000661void
662ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
663{
664 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000665
Greg Clayton5160ce52013-03-27 23:08:40 +0000666 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000667
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000668 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000669 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p",
670 static_cast<void*>(src_ast), static_cast<void*>(dst_ast));
671
Sean Callanan99732312011-11-29 00:42:02 +0000672 if (!md)
673 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000674
Sean Callanan99732312011-11-29 00:42:02 +0000675 md->m_minions.erase(src_ast);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000676
Sean Callanan99732312011-11-29 00:42:02 +0000677 for (OriginMap::iterator iter = md->m_origins.begin();
678 iter != md->m_origins.end();
679 )
680 {
681 if (iter->second.ctx == src_ast)
682 md->m_origins.erase(iter++);
683 else
684 ++iter;
685 }
686}
687
Sean Callanan0eed0d42011-12-06 03:41:14 +0000688ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000689{
690 return;
691}
692
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000693void
Sean Callanane55bc8a2013-03-30 02:31:21 +0000694ClangASTImporter::Minion::InitDeportWorkQueues (std::set<clang::NamedDecl *> *decls_to_deport,
695 std::set<clang::NamedDecl *> *decls_already_deported)
696{
Sean Callananecb17982015-09-02 16:40:59 +0000697 assert(!m_decls_to_deport);
Sean Callanane55bc8a2013-03-30 02:31:21 +0000698 assert(!m_decls_already_deported);
699
700 m_decls_to_deport = decls_to_deport;
701 m_decls_already_deported = decls_already_deported;
702}
703
704void
705ClangASTImporter::Minion::ExecuteDeportWorkQueues ()
706{
Sean Callananecb17982015-09-02 16:40:59 +0000707 assert(m_decls_to_deport);
Sean Callanane55bc8a2013-03-30 02:31:21 +0000708 assert(m_decls_already_deported);
709
710 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&getToContext());
711
712 while (!m_decls_to_deport->empty())
713 {
714 NamedDecl *decl = *m_decls_to_deport->begin();
715
716 m_decls_already_deported->insert(decl);
717 m_decls_to_deport->erase(decl);
718
719 DeclOrigin &origin = to_context_md->m_origins[decl];
Bruce Mitchener8a67bf72015-07-24 00:23:29 +0000720 UNUSED_IF_ASSERT_DISABLED(origin);
Sean Callanane55bc8a2013-03-30 02:31:21 +0000721
722 assert (origin.ctx == m_source_ctx); // otherwise we should never have added this
723 // because it doesn't need to be deported
724
725 Decl *original_decl = to_context_md->m_origins[decl].decl;
726
727 ClangASTContext::GetCompleteDecl (m_source_ctx, original_decl);
728
729 if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
730 {
731 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
732 if (original_tag_decl->isCompleteDefinition())
733 ImportDefinitionTo(tag_decl, original_tag_decl);
734
735 tag_decl->setHasExternalLexicalStorage(false);
736 tag_decl->setHasExternalVisibleStorage(false);
737 }
Sean Callanan3f9de742016-02-10 22:00:32 +0000738 else if (ObjCContainerDecl *container_decl = dyn_cast<ObjCContainerDecl>(decl))
Sean Callanane55bc8a2013-03-30 02:31:21 +0000739 {
Sean Callanan3f9de742016-02-10 22:00:32 +0000740 container_decl->setHasExternalLexicalStorage(false);
741 container_decl->setHasExternalVisibleStorage(false);
Sean Callanane55bc8a2013-03-30 02:31:21 +0000742 }
743
744 to_context_md->m_origins.erase(decl);
745 }
746
Ed Masted4612ad2014-04-20 13:17:36 +0000747 m_decls_to_deport = nullptr;
748 m_decls_already_deported = nullptr;
Sean Callanane55bc8a2013-03-30 02:31:21 +0000749}
750
751void
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000752ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
753{
754 ASTImporter::Imported(from, to);
Sean Callanan5b26f272012-02-04 08:49:35 +0000755
756 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
757
758 /*
759 if (to_objc_interface)
760 to_objc_interface->startDefinition();
761
762 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000763
Sean Callanan5b26f272012-02-04 08:49:35 +0000764 if (to_cxx_record)
765 to_cxx_record->startDefinition();
766 */
767
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000768 ImportDefinition(from);
Sean Callanan5b26f272012-02-04 08:49:35 +0000769
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000770 // If we're dealing with an Objective-C class, ensure that the inheritance has
771 // been set up correctly. The ASTImporter may not do this correctly if the
772 // class was originally sourced from symbols.
773
Sean Callanan5b26f272012-02-04 08:49:35 +0000774 if (to_objc_interface)
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000775 {
776 do
777 {
778 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
779
780 if (to_superclass)
781 break; // we're not going to override it if it's set
782
783 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
784
785 if (!from_objc_interface)
786 break;
787
788 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
789
790 if (!from_superclass)
791 break;
792
793 Decl *imported_from_superclass_decl = Import(from_superclass);
794
795 if (!imported_from_superclass_decl)
796 break;
797
798 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
799
800 if (!imported_from_superclass)
801 break;
802
Sean Callanan5b26f272012-02-04 08:49:35 +0000803 if (!to_objc_interface->hasDefinition())
804 to_objc_interface->startDefinition();
805
Pavel Labath67add942015-07-07 10:11:16 +0000806 to_objc_interface->setSuperClass(
807 m_source_ctx->getTrivialTypeSourceInfo(m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000808 }
809 while (0);
810 }
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000811}
812
Greg Clayton57ee3062013-07-11 22:46:58 +0000813clang::Decl *
814ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
Sean Callanancc427fa2011-07-30 02:42:06 +0000815{
Sean Callanan8106d802013-03-08 20:04:57 +0000816 ClangASTMetrics::RegisterClangImport();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000817
Greg Clayton5160ce52013-03-27 23:08:40 +0000818 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000819
Sean Callanan608fb392014-08-01 22:42:38 +0000820 lldb::user_id_t user_id = LLDB_INVALID_UID;
821 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
822 if (metadata)
823 user_id = metadata->GetUserID();
824
Sean Callanan00f43622011-11-18 03:28:09 +0000825 if (log)
826 {
827 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
828 {
Sean Callanan48e894b2012-05-25 18:12:26 +0000829 std::string name_string;
830 llvm::raw_string_ostream name_stream(name_string);
831 from_named_decl->printName(name_stream);
832 name_stream.flush();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000833
Daniel Malead01b2952012-11-29 21:49:15 +0000834 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000835 from->getDeclKindName(), static_cast<void*>(to),
836 name_string.c_str(), static_cast<void*>(from),
Jim Ingham379397632012-10-27 02:54:13 +0000837 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000838 }
839 else
840 {
Daniel Malead01b2952012-11-29 21:49:15 +0000841 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000842 from->getDeclKindName(), static_cast<void*>(to),
843 static_cast<void*>(from), user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000844 }
845 }
846
Sean Callananb0b87a52011-11-16 22:23:28 +0000847 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
848 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000849
Sean Callananb0b87a52011-11-16 22:23:28 +0000850 if (from_context_md)
851 {
852 OriginMap &origins = from_context_md->m_origins;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000853
Sean Callananb0b87a52011-11-16 22:23:28 +0000854 OriginMap::iterator origin_iter = origins.find(from);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000855
Sean Callananb0b87a52011-11-16 22:23:28 +0000856 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000857 {
Sean Callanan608fb392014-08-01 22:42:38 +0000858 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
859 user_id != LLDB_INVALID_UID)
860 {
Sean Callananabe140c2015-07-20 16:55:19 +0000861 if (origin_iter->second.ctx != &to->getASTContext())
862 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan608fb392014-08-01 22:42:38 +0000863 }
864
Sean Callanan5bc5a762011-12-20 23:55:47 +0000865 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000866
Sean Callanan5bc5a762011-12-20 23:55:47 +0000867 if (direct_completer.get() != this)
868 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000869
Sean Callanan00f43622011-11-18 03:28:09 +0000870 if (log)
871 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000872 static_cast<void*>(origin_iter->second.decl),
873 static_cast<void*>(origin_iter->second.ctx),
874 static_cast<void*>(&from->getASTContext()),
875 static_cast<void*>(&to->getASTContext()));
Sean Callanan00f43622011-11-18 03:28:09 +0000876 }
877 else
878 {
Sean Callanane55bc8a2013-03-30 02:31:21 +0000879 if (m_decls_to_deport && m_decls_already_deported)
880 {
881 if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to))
882 {
Greg Claytone6b36cd2015-12-08 01:02:08 +0000883 RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
884 if (from_record_decl == nullptr || from_record_decl->isInjectedClassName() == false)
885 {
886 NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000887
Greg Claytone6b36cd2015-12-08 01:02:08 +0000888 if (!m_decls_already_deported->count(to_named_decl))
889 m_decls_to_deport->insert(to_named_decl);
890 }
Sean Callanane55bc8a2013-03-30 02:31:21 +0000891 }
Sean Callanane55bc8a2013-03-30 02:31:21 +0000892 }
Sean Callanan608fb392014-08-01 22:42:38 +0000893
894 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
895 user_id != LLDB_INVALID_UID)
896 {
897 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
898 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000899
Sean Callanan00f43622011-11-18 03:28:09 +0000900 if (log)
901 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000902 static_cast<void*>(&from->getASTContext()));
Sean Callanan00f43622011-11-18 03:28:09 +0000903 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000904
Sean Callananb0b87a52011-11-16 22:23:28 +0000905 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
906 {
907 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000908
Sean Callananb0b87a52011-11-16 22:23:28 +0000909 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000910
Sean Callananb0b87a52011-11-16 22:23:28 +0000911 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000912
Sean Callananb0b87a52011-11-16 22:23:28 +0000913 if (namespace_map_iter != namespace_maps.end())
914 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
915 }
916 }
917 else
918 {
919 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000920
Sean Callanan00f43622011-11-18 03:28:09 +0000921 if (log)
922 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000923 static_cast<void*>(from),
924 static_cast<void*>(m_source_ctx),
925 static_cast<void*>(&to->getASTContext()));
Sean Callananb0b87a52011-11-16 22:23:28 +0000926 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000927
Sean Callanancc427fa2011-07-30 02:42:06 +0000928 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
929 {
930 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000931
Sean Callanancc427fa2011-07-30 02:42:06 +0000932 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan3d654b32012-09-24 22:25:51 +0000933 to_tag_decl->setMustBuildLookupTable();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000934
Sean Callanancc427fa2011-07-30 02:42:06 +0000935 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000936 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000937 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000938 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
939 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
940 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanancc427fa2011-07-30 02:42:06 +0000941 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000942
Sean Callananb2269162011-10-21 22:18:07 +0000943 if (isa<NamespaceDecl>(from))
944 {
945 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000946
Sean Callananb2269162011-10-21 22:18:07 +0000947 m_master.BuildNamespaceMap(to_namespace_decl);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000948
Sean Callananb2269162011-10-21 22:18:07 +0000949 to_namespace_decl->setHasExternalVisibleStorage();
950 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000951
Sean Callanan3f9de742016-02-10 22:00:32 +0000952 if (isa<ObjCContainerDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000953 {
Sean Callanan3f9de742016-02-10 22:00:32 +0000954 ObjCContainerDecl *to_container_decl = dyn_cast<ObjCContainerDecl>(to);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000955
Sean Callanan3f9de742016-02-10 22:00:32 +0000956 to_container_decl->setHasExternalLexicalStorage();
957 to_container_decl->setHasExternalVisibleStorage();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000958
Sean Callanan5b26f272012-02-04 08:49:35 +0000959 /*to_interface_decl->setExternallyCompleted();*/
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000960
Sean Callanand5c17ed2011-11-15 02:11:17 +0000961 if (log)
Sean Callanan3f9de742016-02-10 22:00:32 +0000962 {
963 if (ObjCInterfaceDecl *to_interface_decl = llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl))
964 {
965 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
966 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
967 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
968 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
969 }
970 else
971 {
972 log->Printf(" [ClangASTImporter] To is an %sDecl - attributes %s%s",
973 ((Decl*)to_container_decl)->getDeclKindName(),
974 (to_container_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
975 (to_container_decl->hasExternalVisibleStorage() ? " Visible" : ""));
976 }
977 }
Sean Callanancc427fa2011-07-30 02:42:06 +0000978 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000979
Sean Callanancc427fa2011-07-30 02:42:06 +0000980 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000981}
Sean Callanan931a0de2013-10-09 22:33:34 +0000982
983clang::Decl *ClangASTImporter::Minion::GetOriginalDecl (clang::Decl *To)
984{
985 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&To->getASTContext());
986
987 if (!to_context_md)
Ed Masted4612ad2014-04-20 13:17:36 +0000988 return nullptr;
Sean Callanan931a0de2013-10-09 22:33:34 +0000989
990 OriginMap::iterator iter = to_context_md->m_origins.find(To);
991
992 if (iter == to_context_md->m_origins.end())
Ed Masted4612ad2014-04-20 13:17:36 +0000993 return nullptr;
Sean Callanan931a0de2013-10-09 22:33:34 +0000994
995 return const_cast<clang::Decl*>(iter->second.decl);
996}