blob: b12128467de9eec497bed3abfc9695d08242626d [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 {
63 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
Sean Callanand9804fb2012-04-17 22:30:04 +000064 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s', metadata 0x%llx",
65 decl->getDeclKindName(),
66 named_decl->getNameAsString().c_str(),
67 GetDeclMetadata(decl));
Sean Callanand5145b32011-11-05 00:08:12 +000068 else
Sean Callanand9804fb2012-04-17 22:30:04 +000069 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s, metadata 0x%llx",
70 decl->getDeclKindName(),
71 GetDeclMetadata(decl));
Sean Callanand5145b32011-11-05 00:08:12 +000072 }
Sean Callananbfb237bc2011-11-04 22:46:46 +000073 }
74
75 return result;
76 }
Sean Callanancc427fa2011-07-30 02:42:06 +000077
Greg Claytondd0649b2011-07-06 18:55:08 +000078 return NULL;
Greg Claytona2721472011-06-25 00:44:06 +000079}
80
Sean Callananbb120042011-12-16 21:06:35 +000081lldb::clang_type_t
82ClangASTImporter::DeportType (clang::ASTContext *dst_ctx,
83 clang::ASTContext *src_ctx,
84 lldb::clang_type_t type)
85{
86 lldb::clang_type_t result = CopyType(dst_ctx, src_ctx, type);
87
88 if (!result)
89 return NULL;
90
91 QualType qual_type = QualType::getFromOpaquePtr(type);
92
93 if (const TagType *tag_type = qual_type->getAs<TagType>())
94 {
95 TagDecl *tag_decl = tag_type->getDecl();
96 const TagType *result_tag_type = QualType::getFromOpaquePtr(result)->getAs<TagType>();
97 TagDecl *result_tag_decl = result_tag_type->getDecl();
98
99 if (tag_decl)
100 {
101 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
102
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000103 minion_sp->ImportDefinitionTo(result_tag_decl, tag_decl);
Sean Callananbb120042011-12-16 21:06:35 +0000104
105 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
106
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000107 OriginMap::iterator oi = to_context_md->m_origins.find(result_tag_decl);
108
109 if (oi != to_context_md->m_origins.end() &&
110 oi->second.ctx == src_ctx)
111 to_context_md->m_origins.erase(oi);
Sean Callananbb120042011-12-16 21:06:35 +0000112 }
113 }
114
115 return result;
116}
117
Sean Callanan0eed0d42011-12-06 03:41:14 +0000118clang::Decl *
119ClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx,
120 clang::ASTContext *src_ctx,
121 clang::Decl *decl)
122{
123 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
124
125 if (!result)
126 return NULL;
127
128 ClangASTContext::GetCompleteDecl (src_ctx, decl);
129
130 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
131
132 if (minion_sp && isa<TagDecl>(decl))
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000133 minion_sp->ImportDefinitionTo(result, decl);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000134
135 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
136
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000137 OriginMap::iterator oi = to_context_md->m_origins.find(decl);
138
139 if (oi != to_context_md->m_origins.end() &&
140 oi->second.ctx == src_ctx)
141 to_context_md->m_origins.erase(oi);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000142
143 return result;
144}
145
Sean Callanan5b26f272012-02-04 08:49:35 +0000146void
147ClangASTImporter::CompleteDecl (clang::Decl *decl)
148{
149 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
150
151 if (log)
152 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
153 decl->getDeclKindName(),
154 decl);
155
156 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
157 {
158 if (!interface_decl->getDefinition())
159 {
160 interface_decl->startDefinition();
161 CompleteObjCInterfaceDecl(interface_decl);
162 }
163 }
Greg Clayton23f59502012-07-17 03:23:13 +0000164 else if (ObjCProtocolDecl *protocol_decl = dyn_cast<ObjCProtocolDecl>(decl))
Sean Callanan5b26f272012-02-04 08:49:35 +0000165 {
166 if (!protocol_decl->getDefinition())
167 protocol_decl->startDefinition();
168 }
169 else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
170 {
171 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined())
172 {
173 tag_decl->startDefinition();
174 CompleteTagDecl(tag_decl);
175 tag_decl->setCompleteDefinition(true);
176 }
177 }
Greg Clayton219cf312012-03-30 00:51:13 +0000178 else
179 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000180 assert (0 && "CompleteDecl called on a Decl that can't be completed");
181 }
182}
183
Sean Callanan12014a02011-12-08 23:45:45 +0000184bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000185ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Sean Callanan12014a02011-12-08 23:45:45 +0000186{
Sean Callanancc427fa2011-07-30 02:42:06 +0000187 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +0000188
Sean Callanancc427fa2011-07-30 02:42:06 +0000189 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000190 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000191
Sean Callanancc427fa2011-07-30 02:42:06 +0000192 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000193 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000194
Sean Callanan686b2312011-11-16 18:20:47 +0000195 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
Greg Claytona2721472011-06-25 00:44:06 +0000196
Greg Claytondd0649b2011-07-06 18:55:08 +0000197 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000198 minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
199
Sean Callanan12014a02011-12-08 23:45:45 +0000200 return true;
Greg Claytona2721472011-06-25 00:44:06 +0000201}
Sean Callanancc427fa2011-07-30 02:42:06 +0000202
Sean Callanan12014a02011-12-08 23:45:45 +0000203bool
204ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
205{
206 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
207
208 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
209 return false;
210
211 MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
212
213 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000214 minion_sp->ImportDefinitionTo(decl, origin_decl);
215
Sean Callanan12014a02011-12-08 23:45:45 +0000216 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
217
218 OriginMap &origins = context_md->m_origins;
219
220 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
221
222 return true;
223}
224
225bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000226ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
227{
228 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
229
Sean Callanancc427fa2011-07-30 02:42:06 +0000230 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
231
232 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000233 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000234
235 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000236 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000237
Sean Callanan686b2312011-11-16 18:20:47 +0000238 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000239
240 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000241 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
242
Sean Callanan12014a02011-12-08 23:45:45 +0000243 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000244}
245
Sean Callanan60217122012-04-13 00:10:03 +0000246uint64_t
247ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
248{
249 DeclOrigin decl_origin = GetDeclOrigin(decl);
250
251 if (decl_origin.Valid())
252 return ClangASTContext::GetMetadata(decl_origin.ctx, (uintptr_t)decl_origin.decl);
253 else
254 return ClangASTContext::GetMetadata(&decl->getASTContext(), (uintptr_t)decl);
255}
256
Sean Callananf487bd82011-11-16 21:40:57 +0000257ClangASTImporter::DeclOrigin
258ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
259{
260 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
261
262 OriginMap &origins = context_md->m_origins;
263
264 OriginMap::iterator iter = origins.find(decl);
265
266 if (iter != origins.end())
267 return iter->second;
268 else
269 return DeclOrigin();
270}
271
Sean Callanan2cb5e522012-09-20 23:21:16 +0000272void
273ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
274{
275 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
276
277 OriginMap &origins = context_md->m_origins;
278
279 OriginMap::iterator iter = origins.find(decl);
280
281 if (iter != origins.end())
282 {
283 iter->second.decl = original_decl;
284 iter->second.ctx = &original_decl->getASTContext();
285 }
286 else
287 {
288 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
289 }
290}
291
292void
Sean Callanan503aa522011-10-12 00:12:34 +0000293ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
294 NamespaceMapSP &namespace_map)
295{
Sean Callananf487bd82011-11-16 21:40:57 +0000296 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
297
298 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000299}
300
301ClangASTImporter::NamespaceMapSP
302ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
303{
Sean Callananf487bd82011-11-16 21:40:57 +0000304 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
305
306 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000307
Sean Callananf487bd82011-11-16 21:40:57 +0000308 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
309
310 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000311 return iter->second;
312 else
313 return NamespaceMapSP();
314}
315
Sean Callananb2269162011-10-21 22:18:07 +0000316void
317ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
318{
Jim Ingham28eb5712012-10-12 17:34:26 +0000319 assert (decl);
Sean Callananf487bd82011-11-16 21:40:57 +0000320 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
321
Sean Callananb2269162011-10-21 22:18:07 +0000322 const DeclContext *parent_context = decl->getDeclContext();
323 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
324 NamespaceMapSP parent_map;
325
326 if (parent_namespace)
327 parent_map = GetNamespaceMap(parent_namespace);
328
329 NamespaceMapSP new_map;
330
331 new_map.reset(new NamespaceMap);
332
Sean Callananf487bd82011-11-16 21:40:57 +0000333 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000334 {
335 std::string namespace_string = decl->getDeclName().getAsString();
336
Sean Callananf487bd82011-11-16 21:40:57 +0000337 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000338 }
339
Sean Callanan0eed0d42011-12-06 03:41:14 +0000340 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000341}
342
Sean Callanan686b2312011-11-16 18:20:47 +0000343void
Sean Callanan99732312011-11-29 00:42:02 +0000344ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000345{
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000346 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
347
348 if (log)
349 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p", dst_ast);
350
Sean Callananf487bd82011-11-16 21:40:57 +0000351 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000352}
353
Sean Callanan99732312011-11-29 00:42:02 +0000354void
355ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
356{
357 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
358
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000359 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
360
361 if (log)
362 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p", src_ast, dst_ast);
363
Sean Callanan99732312011-11-29 00:42:02 +0000364 if (!md)
365 return;
366
367 md->m_minions.erase(src_ast);
368
369 for (OriginMap::iterator iter = md->m_origins.begin();
370 iter != md->m_origins.end();
371 )
372 {
373 if (iter->second.ctx == src_ast)
374 md->m_origins.erase(iter++);
375 else
376 ++iter;
377 }
378}
379
Sean Callanan0eed0d42011-12-06 03:41:14 +0000380ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000381{
382 return;
383}
384
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000385void
386ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
387{
388 ASTImporter::Imported(from, to);
Sean Callanan5b26f272012-02-04 08:49:35 +0000389
390 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
391
392 /*
393 if (to_objc_interface)
394 to_objc_interface->startDefinition();
395
396 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000397
Sean Callanan5b26f272012-02-04 08:49:35 +0000398 if (to_cxx_record)
399 to_cxx_record->startDefinition();
400 */
401
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000402 ImportDefinition(from);
Sean Callanan5b26f272012-02-04 08:49:35 +0000403
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000404 // If we're dealing with an Objective-C class, ensure that the inheritance has
405 // been set up correctly. The ASTImporter may not do this correctly if the
406 // class was originally sourced from symbols.
407
Sean Callanan5b26f272012-02-04 08:49:35 +0000408 if (to_objc_interface)
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000409 {
410 do
411 {
412 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
413
414 if (to_superclass)
415 break; // we're not going to override it if it's set
416
417 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
418
419 if (!from_objc_interface)
420 break;
421
422 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
423
424 if (!from_superclass)
425 break;
426
427 Decl *imported_from_superclass_decl = Import(from_superclass);
428
429 if (!imported_from_superclass_decl)
430 break;
431
432 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
433
434 if (!imported_from_superclass)
435 break;
436
Sean Callanan5b26f272012-02-04 08:49:35 +0000437 if (!to_objc_interface->hasDefinition())
438 to_objc_interface->startDefinition();
439
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000440 to_objc_interface->setSuperClass(imported_from_superclass);
441 }
442 while (0);
443 }
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000444}
445
Sean Callanancc427fa2011-07-30 02:42:06 +0000446clang::Decl
447*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
448{
449 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000450
451 if (log)
452 {
453 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
454 {
Sean Callanan48e894b2012-05-25 18:12:26 +0000455 std::string name_string;
456 llvm::raw_string_ostream name_stream(name_string);
457 from_named_decl->printName(name_stream);
458 name_stream.flush();
459
Sean Callanan60217122012-04-13 00:10:03 +0000460 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%llx",
Sean Callanan00f43622011-11-18 03:28:09 +0000461 from->getDeclKindName(),
462 to,
Sean Callanan48e894b2012-05-25 18:12:26 +0000463 name_string.c_str(),
Sean Callanan60217122012-04-13 00:10:03 +0000464 from,
465 m_master.GetDeclMetadata(from));
Sean Callanan00f43622011-11-18 03:28:09 +0000466 }
467 else
468 {
Sean Callanan60217122012-04-13 00:10:03 +0000469 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%llx",
Sean Callanan00f43622011-11-18 03:28:09 +0000470 from->getDeclKindName(),
471 to,
Sean Callanan60217122012-04-13 00:10:03 +0000472 from,
473 m_master.GetDeclMetadata(from));
Sean Callanan00f43622011-11-18 03:28:09 +0000474 }
475 }
476
Sean Callananb0b87a52011-11-16 22:23:28 +0000477 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
478 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000479
Sean Callananb0b87a52011-11-16 22:23:28 +0000480 if (from_context_md)
481 {
482 OriginMap &origins = from_context_md->m_origins;
483
484 OriginMap::iterator origin_iter = origins.find(from);
485
486 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000487 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000488 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000489
Sean Callanan5bc5a762011-12-20 23:55:47 +0000490 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
491
492 if (direct_completer.get() != this)
493 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
494
Sean Callanan00f43622011-11-18 03:28:09 +0000495 if (log)
496 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
497 origin_iter->second.decl,
498 origin_iter->second.ctx,
499 &from->getASTContext(),
500 &to->getASTContext());
501 }
502 else
503 {
Sean Callanan0eed0d42011-12-06 03:41:14 +0000504 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
505
Sean Callanan00f43622011-11-18 03:28:09 +0000506 if (log)
507 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
508 &from->getASTContext());
509 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000510
511 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
512 {
513 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
514
515 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
516
517 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
518
519 if (namespace_map_iter != namespace_maps.end())
520 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
521 }
522 }
523 else
524 {
525 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000526
527 if (log)
528 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
529 from,
530 m_source_ctx,
531 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000532 }
533
Sean Callanancc427fa2011-07-30 02:42:06 +0000534 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
535 {
536 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
537
538 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan3d654b32012-09-24 22:25:51 +0000539 to_tag_decl->setMustBuildLookupTable();
540
Sean Callanancc427fa2011-07-30 02:42:06 +0000541 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000542 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000543 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000544 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
545 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
546 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000547
548 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000549 }
550
Sean Callananb2269162011-10-21 22:18:07 +0000551 if (isa<NamespaceDecl>(from))
552 {
553 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
554
555 m_master.BuildNamespaceMap(to_namespace_decl);
556
557 to_namespace_decl->setHasExternalVisibleStorage();
558 }
559
Sean Callanan00f43622011-11-18 03:28:09 +0000560 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000561 {
562 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
Sean Callanana9bc0652012-01-19 02:17:40 +0000563
Sean Callanand5c17ed2011-11-15 02:11:17 +0000564 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000565 to_interface_decl->setHasExternalVisibleStorage();
Sean Callanan5b26f272012-02-04 08:49:35 +0000566
567 /*to_interface_decl->setExternallyCompleted();*/
Sean Callanan0eed0d42011-12-06 03:41:14 +0000568
Sean Callanand5c17ed2011-11-15 02:11:17 +0000569 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000570 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000571 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
572 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
Sean Callanan5b26f272012-02-04 08:49:35 +0000573 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000574 }
575
576 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000577}