blob: bd669fccf21e9f4f98d22ac016962133288e8a45 [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"
13#include "lldb/Core/Log.h"
Greg Clayton7c6d7b82011-10-21 23:04:20 +000014#include "lldb/Core/Module.h"
Greg Claytona2721472011-06-25 00:44:06 +000015#include "lldb/Symbol/ClangASTContext.h"
16#include "lldb/Symbol/ClangASTImporter.h"
Sean Callanan60217122012-04-13 00:10:03 +000017#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callananb2269162011-10-21 22:18:07 +000018#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Claytona2721472011-06-25 00:44:06 +000019
20using namespace lldb_private;
21using namespace clang;
22
23clang::QualType
Sean Callanan686b2312011-11-16 18:20:47 +000024ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
25 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000026 clang::QualType type)
27{
Sean Callanan686b2312011-11-16 18:20:47 +000028 MinionSP minion_sp (GetMinion(dst_ast, src_ast));
Sean Callanancc427fa2011-07-30 02:42:06 +000029
Greg Claytondd0649b2011-07-06 18:55:08 +000030 if (minion_sp)
31 return minion_sp->Import(type);
Sean Callanancc427fa2011-07-30 02:42:06 +000032
Greg Claytondd0649b2011-07-06 18:55:08 +000033 return QualType();
Greg Claytona2721472011-06-25 00:44:06 +000034}
35
Sean Callanan80f78672011-11-16 19:07:39 +000036lldb::clang_type_t
37ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
38 clang::ASTContext *src_ast,
39 lldb::clang_type_t type)
40{
41 return CopyType (dst_ast, src_ast, QualType::getFromOpaquePtr(type)).getAsOpaquePtr();
42}
43
Greg Claytona2721472011-06-25 00:44:06 +000044clang::Decl *
Sean Callanan686b2312011-11-16 18:20:47 +000045ClangASTImporter::CopyDecl (clang::ASTContext *dst_ast,
46 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000047 clang::Decl *decl)
48{
Greg Claytondd0649b2011-07-06 18:55:08 +000049 MinionSP minion_sp;
Greg Claytona2721472011-06-25 00:44:06 +000050
Sean Callanan686b2312011-11-16 18:20:47 +000051 minion_sp = GetMinion(dst_ast, src_ast);
Greg Claytona2721472011-06-25 00:44:06 +000052
Greg Claytondd0649b2011-07-06 18:55:08 +000053 if (minion_sp)
Sean Callananbfb237bc2011-11-04 22:46:46 +000054 {
55 clang::Decl *result = minion_sp->Import(decl);
56
57 if (!result)
58 {
59 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
60
Sean Callanand5145b32011-11-05 00:08:12 +000061 if (log)
62 {
Jim Ingham379397632012-10-27 02:54:13 +000063 lldb::user_id_t user_id;
64 ClangASTMetadata *metadata = GetDeclMetadata(decl);
65 if (metadata)
66 user_id = metadata->GetUserID();
67
Sean Callanand5145b32011-11-05 00:08:12 +000068 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
Sean Callanand9804fb2012-04-17 22:30:04 +000069 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s', metadata 0x%llx",
70 decl->getDeclKindName(),
71 named_decl->getNameAsString().c_str(),
Jim Ingham379397632012-10-27 02:54:13 +000072 user_id);
Sean Callanand5145b32011-11-05 00:08:12 +000073 else
Sean Callanand9804fb2012-04-17 22:30:04 +000074 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s, metadata 0x%llx",
75 decl->getDeclKindName(),
Jim Ingham379397632012-10-27 02:54:13 +000076 user_id);
Sean Callanand5145b32011-11-05 00:08:12 +000077 }
Sean Callananbfb237bc2011-11-04 22:46:46 +000078 }
79
80 return result;
81 }
Sean Callanancc427fa2011-07-30 02:42:06 +000082
Greg Claytondd0649b2011-07-06 18:55:08 +000083 return NULL;
Greg Claytona2721472011-06-25 00:44:06 +000084}
85
Sean Callananbb120042011-12-16 21:06:35 +000086lldb::clang_type_t
87ClangASTImporter::DeportType (clang::ASTContext *dst_ctx,
88 clang::ASTContext *src_ctx,
89 lldb::clang_type_t type)
90{
91 lldb::clang_type_t result = CopyType(dst_ctx, src_ctx, type);
92
93 if (!result)
94 return NULL;
95
96 QualType qual_type = QualType::getFromOpaquePtr(type);
97
98 if (const TagType *tag_type = qual_type->getAs<TagType>())
99 {
100 TagDecl *tag_decl = tag_type->getDecl();
101 const TagType *result_tag_type = QualType::getFromOpaquePtr(result)->getAs<TagType>();
102 TagDecl *result_tag_decl = result_tag_type->getDecl();
103
104 if (tag_decl)
105 {
106 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
107
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000108 minion_sp->ImportDefinitionTo(result_tag_decl, tag_decl);
Sean Callananbb120042011-12-16 21:06:35 +0000109
110 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
111
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000112 OriginMap::iterator oi = to_context_md->m_origins.find(result_tag_decl);
113
114 if (oi != to_context_md->m_origins.end() &&
115 oi->second.ctx == src_ctx)
116 to_context_md->m_origins.erase(oi);
Sean Callananbb120042011-12-16 21:06:35 +0000117 }
118 }
119
120 return result;
121}
122
Sean Callanan0eed0d42011-12-06 03:41:14 +0000123clang::Decl *
124ClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx,
125 clang::ASTContext *src_ctx,
126 clang::Decl *decl)
127{
128 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
129
130 if (!result)
131 return NULL;
132
133 ClangASTContext::GetCompleteDecl (src_ctx, decl);
134
135 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
136
137 if (minion_sp && isa<TagDecl>(decl))
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000138 minion_sp->ImportDefinitionTo(result, decl);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000139
140 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
141
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000142 OriginMap::iterator oi = to_context_md->m_origins.find(decl);
143
144 if (oi != to_context_md->m_origins.end() &&
145 oi->second.ctx == src_ctx)
146 to_context_md->m_origins.erase(oi);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000147
148 return result;
149}
150
Sean Callanan5b26f272012-02-04 08:49:35 +0000151void
152ClangASTImporter::CompleteDecl (clang::Decl *decl)
153{
154 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
155
156 if (log)
157 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
158 decl->getDeclKindName(),
159 decl);
160
161 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
162 {
163 if (!interface_decl->getDefinition())
164 {
165 interface_decl->startDefinition();
166 CompleteObjCInterfaceDecl(interface_decl);
167 }
168 }
Greg Clayton23f59502012-07-17 03:23:13 +0000169 else if (ObjCProtocolDecl *protocol_decl = dyn_cast<ObjCProtocolDecl>(decl))
Sean Callanan5b26f272012-02-04 08:49:35 +0000170 {
171 if (!protocol_decl->getDefinition())
172 protocol_decl->startDefinition();
173 }
174 else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
175 {
176 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined())
177 {
178 tag_decl->startDefinition();
179 CompleteTagDecl(tag_decl);
180 tag_decl->setCompleteDefinition(true);
181 }
182 }
Greg Clayton219cf312012-03-30 00:51:13 +0000183 else
184 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000185 assert (0 && "CompleteDecl called on a Decl that can't be completed");
186 }
187}
188
Sean Callanan12014a02011-12-08 23:45:45 +0000189bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000190ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Sean Callanan12014a02011-12-08 23:45:45 +0000191{
Sean Callanancc427fa2011-07-30 02:42:06 +0000192 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +0000193
Sean Callanancc427fa2011-07-30 02:42:06 +0000194 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000195 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000196
Sean Callanancc427fa2011-07-30 02:42:06 +0000197 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000198 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000199
Sean Callanan686b2312011-11-16 18:20:47 +0000200 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
Greg Claytona2721472011-06-25 00:44:06 +0000201
Greg Claytondd0649b2011-07-06 18:55:08 +0000202 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000203 minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
204
Sean Callanan12014a02011-12-08 23:45:45 +0000205 return true;
Greg Claytona2721472011-06-25 00:44:06 +0000206}
Sean Callanancc427fa2011-07-30 02:42:06 +0000207
Sean Callanan12014a02011-12-08 23:45:45 +0000208bool
209ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
210{
211 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
212
213 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
214 return false;
215
216 MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
217
218 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000219 minion_sp->ImportDefinitionTo(decl, origin_decl);
220
Sean Callanan12014a02011-12-08 23:45:45 +0000221 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
222
223 OriginMap &origins = context_md->m_origins;
224
225 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
226
227 return true;
228}
229
230bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000231ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
232{
233 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
234
Sean Callanancc427fa2011-07-30 02:42:06 +0000235 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
236
237 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000238 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000239
240 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000241 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000242
Sean Callanan686b2312011-11-16 18:20:47 +0000243 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000244
245 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000246 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
247
Sean Callanan12014a02011-12-08 23:45:45 +0000248 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000249}
250
Jim Ingham379397632012-10-27 02:54:13 +0000251ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +0000252ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
253{
254 DeclOrigin decl_origin = GetDeclOrigin(decl);
255
256 if (decl_origin.Valid())
257 return ClangASTContext::GetMetadata(decl_origin.ctx, (uintptr_t)decl_origin.decl);
258 else
259 return ClangASTContext::GetMetadata(&decl->getASTContext(), (uintptr_t)decl);
260}
261
Sean Callananf487bd82011-11-16 21:40:57 +0000262ClangASTImporter::DeclOrigin
263ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
264{
265 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
266
267 OriginMap &origins = context_md->m_origins;
268
269 OriginMap::iterator iter = origins.find(decl);
270
271 if (iter != origins.end())
272 return iter->second;
273 else
274 return DeclOrigin();
275}
276
Sean Callanan2cb5e522012-09-20 23:21:16 +0000277void
278ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
279{
280 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
281
282 OriginMap &origins = context_md->m_origins;
283
284 OriginMap::iterator iter = origins.find(decl);
285
286 if (iter != origins.end())
287 {
288 iter->second.decl = original_decl;
289 iter->second.ctx = &original_decl->getASTContext();
290 }
291 else
292 {
293 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
294 }
295}
296
297void
Sean Callanan503aa522011-10-12 00:12:34 +0000298ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
299 NamespaceMapSP &namespace_map)
300{
Sean Callananf487bd82011-11-16 21:40:57 +0000301 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
302
303 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000304}
305
306ClangASTImporter::NamespaceMapSP
307ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
308{
Sean Callananf487bd82011-11-16 21:40:57 +0000309 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
310
311 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000312
Sean Callananf487bd82011-11-16 21:40:57 +0000313 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
314
315 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000316 return iter->second;
317 else
318 return NamespaceMapSP();
319}
320
Sean Callananb2269162011-10-21 22:18:07 +0000321void
322ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
323{
Jim Ingham28eb5712012-10-12 17:34:26 +0000324 assert (decl);
Sean Callananf487bd82011-11-16 21:40:57 +0000325 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
326
Sean Callananb2269162011-10-21 22:18:07 +0000327 const DeclContext *parent_context = decl->getDeclContext();
328 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
329 NamespaceMapSP parent_map;
330
331 if (parent_namespace)
332 parent_map = GetNamespaceMap(parent_namespace);
333
334 NamespaceMapSP new_map;
335
336 new_map.reset(new NamespaceMap);
337
Sean Callananf487bd82011-11-16 21:40:57 +0000338 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000339 {
340 std::string namespace_string = decl->getDeclName().getAsString();
341
Sean Callananf487bd82011-11-16 21:40:57 +0000342 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000343 }
344
Sean Callanan0eed0d42011-12-06 03:41:14 +0000345 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000346}
347
Sean Callanan686b2312011-11-16 18:20:47 +0000348void
Sean Callanan99732312011-11-29 00:42:02 +0000349ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000350{
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000351 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
352
353 if (log)
354 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p", dst_ast);
355
Sean Callananf487bd82011-11-16 21:40:57 +0000356 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000357}
358
Sean Callanan99732312011-11-29 00:42:02 +0000359void
360ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
361{
362 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
363
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000364 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
365
366 if (log)
367 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p", src_ast, dst_ast);
368
Sean Callanan99732312011-11-29 00:42:02 +0000369 if (!md)
370 return;
371
372 md->m_minions.erase(src_ast);
373
374 for (OriginMap::iterator iter = md->m_origins.begin();
375 iter != md->m_origins.end();
376 )
377 {
378 if (iter->second.ctx == src_ast)
379 md->m_origins.erase(iter++);
380 else
381 ++iter;
382 }
383}
384
Sean Callanan0eed0d42011-12-06 03:41:14 +0000385ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000386{
387 return;
388}
389
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000390void
391ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
392{
393 ASTImporter::Imported(from, to);
Sean Callanan5b26f272012-02-04 08:49:35 +0000394
395 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
396
397 /*
398 if (to_objc_interface)
399 to_objc_interface->startDefinition();
400
401 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000402
Sean Callanan5b26f272012-02-04 08:49:35 +0000403 if (to_cxx_record)
404 to_cxx_record->startDefinition();
405 */
406
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000407 ImportDefinition(from);
Sean Callanan5b26f272012-02-04 08:49:35 +0000408
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000409 // If we're dealing with an Objective-C class, ensure that the inheritance has
410 // been set up correctly. The ASTImporter may not do this correctly if the
411 // class was originally sourced from symbols.
412
Sean Callanan5b26f272012-02-04 08:49:35 +0000413 if (to_objc_interface)
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000414 {
415 do
416 {
417 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
418
419 if (to_superclass)
420 break; // we're not going to override it if it's set
421
422 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
423
424 if (!from_objc_interface)
425 break;
426
427 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
428
429 if (!from_superclass)
430 break;
431
432 Decl *imported_from_superclass_decl = Import(from_superclass);
433
434 if (!imported_from_superclass_decl)
435 break;
436
437 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
438
439 if (!imported_from_superclass)
440 break;
441
Sean Callanan5b26f272012-02-04 08:49:35 +0000442 if (!to_objc_interface->hasDefinition())
443 to_objc_interface->startDefinition();
444
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000445 to_objc_interface->setSuperClass(imported_from_superclass);
446 }
447 while (0);
448 }
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000449}
450
Sean Callanancc427fa2011-07-30 02:42:06 +0000451clang::Decl
452*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
453{
454 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000455
456 if (log)
457 {
Jim Ingham379397632012-10-27 02:54:13 +0000458 lldb::user_id_t user_id;
459 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
460 if (metadata)
461 user_id = metadata->GetUserID();
462
Sean Callanan00f43622011-11-18 03:28:09 +0000463 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
464 {
Sean Callanan48e894b2012-05-25 18:12:26 +0000465 std::string name_string;
466 llvm::raw_string_ostream name_stream(name_string);
467 from_named_decl->printName(name_stream);
468 name_stream.flush();
469
Sean Callanan60217122012-04-13 00:10:03 +0000470 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%llx",
Sean Callanan00f43622011-11-18 03:28:09 +0000471 from->getDeclKindName(),
472 to,
Sean Callanan48e894b2012-05-25 18:12:26 +0000473 name_string.c_str(),
Sean Callanan60217122012-04-13 00:10:03 +0000474 from,
Jim Ingham379397632012-10-27 02:54:13 +0000475 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000476 }
477 else
478 {
Sean Callanan60217122012-04-13 00:10:03 +0000479 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%llx",
Sean Callanan00f43622011-11-18 03:28:09 +0000480 from->getDeclKindName(),
481 to,
Sean Callanan60217122012-04-13 00:10:03 +0000482 from,
Jim Ingham379397632012-10-27 02:54:13 +0000483 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000484 }
485 }
486
Sean Callananb0b87a52011-11-16 22:23:28 +0000487 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
488 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000489
Sean Callananb0b87a52011-11-16 22:23:28 +0000490 if (from_context_md)
491 {
492 OriginMap &origins = from_context_md->m_origins;
493
494 OriginMap::iterator origin_iter = origins.find(from);
495
496 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000497 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000498 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000499
Sean Callanan5bc5a762011-12-20 23:55:47 +0000500 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
501
502 if (direct_completer.get() != this)
503 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
504
Sean Callanan00f43622011-11-18 03:28:09 +0000505 if (log)
506 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
507 origin_iter->second.decl,
508 origin_iter->second.ctx,
509 &from->getASTContext(),
510 &to->getASTContext());
511 }
512 else
513 {
Sean Callanan0eed0d42011-12-06 03:41:14 +0000514 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
515
Sean Callanan00f43622011-11-18 03:28:09 +0000516 if (log)
517 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
518 &from->getASTContext());
519 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000520
521 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
522 {
523 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
524
525 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
526
527 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
528
529 if (namespace_map_iter != namespace_maps.end())
530 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
531 }
532 }
533 else
534 {
535 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000536
537 if (log)
538 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
539 from,
540 m_source_ctx,
541 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000542 }
543
Sean Callanancc427fa2011-07-30 02:42:06 +0000544 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
545 {
546 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
547
548 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan3d654b32012-09-24 22:25:51 +0000549 to_tag_decl->setMustBuildLookupTable();
550
Sean Callanancc427fa2011-07-30 02:42:06 +0000551 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000552 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000553 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000554 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
555 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
556 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000557
558 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000559 }
560
Sean Callananb2269162011-10-21 22:18:07 +0000561 if (isa<NamespaceDecl>(from))
562 {
563 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
564
565 m_master.BuildNamespaceMap(to_namespace_decl);
566
567 to_namespace_decl->setHasExternalVisibleStorage();
568 }
569
Sean Callanan00f43622011-11-18 03:28:09 +0000570 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000571 {
572 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
Sean Callanana9bc0652012-01-19 02:17:40 +0000573
Sean Callanand5c17ed2011-11-15 02:11:17 +0000574 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000575 to_interface_decl->setHasExternalVisibleStorage();
Sean Callanan5b26f272012-02-04 08:49:35 +0000576
577 /*to_interface_decl->setExternallyCompleted();*/
Sean Callanan0eed0d42011-12-06 03:41:14 +0000578
Sean Callanand5c17ed2011-11-15 02:11:17 +0000579 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000580 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000581 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
582 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
Sean Callanan5b26f272012-02-04 08:49:35 +0000583 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000584 }
585
586 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000587}