blob: 2a0aa8a8c6bf36fc85cf3f82fa077f5f5b1d8b90 [file] [log] [blame]
Greg Claytona2721472011-06-25 00:44:06 +00001//===-- ClangASTImporter.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/AST/Decl.h"
Sean Callanancbbe3ac2012-01-13 22:55:55 +000011#include "clang/AST/DeclCXX.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000012#include "clang/AST/DeclObjC.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000013#include "llvm/Support/raw_ostream.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000014#include "lldb/Core/Log.h"
Greg Clayton7c6d7b82011-10-21 23:04:20 +000015#include "lldb/Core/Module.h"
Greg Claytona2721472011-06-25 00:44:06 +000016#include "lldb/Symbol/ClangASTContext.h"
17#include "lldb/Symbol/ClangASTImporter.h"
Sean Callanan60217122012-04-13 00:10:03 +000018#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callananb2269162011-10-21 22:18:07 +000019#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Claytona2721472011-06-25 00:44:06 +000020
21using namespace lldb_private;
22using namespace clang;
23
24clang::QualType
Sean Callanan686b2312011-11-16 18:20:47 +000025ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
26 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000027 clang::QualType type)
28{
Sean Callanan686b2312011-11-16 18:20:47 +000029 MinionSP minion_sp (GetMinion(dst_ast, src_ast));
Sean Callanancc427fa2011-07-30 02:42:06 +000030
Greg Claytondd0649b2011-07-06 18:55:08 +000031 if (minion_sp)
32 return minion_sp->Import(type);
Sean Callanancc427fa2011-07-30 02:42:06 +000033
Greg Claytondd0649b2011-07-06 18:55:08 +000034 return QualType();
Greg Claytona2721472011-06-25 00:44:06 +000035}
36
Sean Callanan80f78672011-11-16 19:07:39 +000037lldb::clang_type_t
38ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
39 clang::ASTContext *src_ast,
40 lldb::clang_type_t type)
41{
42 return CopyType (dst_ast, src_ast, QualType::getFromOpaquePtr(type)).getAsOpaquePtr();
43}
44
Greg Claytona2721472011-06-25 00:44:06 +000045clang::Decl *
Sean Callanan686b2312011-11-16 18:20:47 +000046ClangASTImporter::CopyDecl (clang::ASTContext *dst_ast,
47 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000048 clang::Decl *decl)
49{
Greg Claytondd0649b2011-07-06 18:55:08 +000050 MinionSP minion_sp;
Greg Claytona2721472011-06-25 00:44:06 +000051
Sean Callanan686b2312011-11-16 18:20:47 +000052 minion_sp = GetMinion(dst_ast, src_ast);
Greg Claytona2721472011-06-25 00:44:06 +000053
Greg Claytondd0649b2011-07-06 18:55:08 +000054 if (minion_sp)
Sean Callananbfb237bc2011-11-04 22:46:46 +000055 {
56 clang::Decl *result = minion_sp->Import(decl);
57
58 if (!result)
59 {
60 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
61
Sean Callanand5145b32011-11-05 00:08:12 +000062 if (log)
63 {
Jim Ingham379397632012-10-27 02:54:13 +000064 lldb::user_id_t user_id;
65 ClangASTMetadata *metadata = GetDeclMetadata(decl);
66 if (metadata)
67 user_id = metadata->GetUserID();
68
Sean Callanand5145b32011-11-05 00:08:12 +000069 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
Daniel Malead01b2952012-11-29 21:49:15 +000070 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s', metadata 0x%" PRIx64,
Sean Callanand9804fb2012-04-17 22:30:04 +000071 decl->getDeclKindName(),
72 named_decl->getNameAsString().c_str(),
Jim Ingham379397632012-10-27 02:54:13 +000073 user_id);
Sean Callanand5145b32011-11-05 00:08:12 +000074 else
Daniel Malead01b2952012-11-29 21:49:15 +000075 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s, metadata 0x%" PRIx64,
Sean Callanand9804fb2012-04-17 22:30:04 +000076 decl->getDeclKindName(),
Jim Ingham379397632012-10-27 02:54:13 +000077 user_id);
Sean Callanand5145b32011-11-05 00:08:12 +000078 }
Sean Callananbfb237bc2011-11-04 22:46:46 +000079 }
80
81 return result;
82 }
Sean Callanancc427fa2011-07-30 02:42:06 +000083
Greg Claytondd0649b2011-07-06 18:55:08 +000084 return NULL;
Greg Claytona2721472011-06-25 00:44:06 +000085}
86
Sean Callananbb120042011-12-16 21:06:35 +000087lldb::clang_type_t
88ClangASTImporter::DeportType (clang::ASTContext *dst_ctx,
89 clang::ASTContext *src_ctx,
90 lldb::clang_type_t type)
91{
92 lldb::clang_type_t result = CopyType(dst_ctx, src_ctx, type);
93
94 if (!result)
95 return NULL;
96
97 QualType qual_type = QualType::getFromOpaquePtr(type);
98
99 if (const TagType *tag_type = qual_type->getAs<TagType>())
100 {
101 TagDecl *tag_decl = tag_type->getDecl();
102 const TagType *result_tag_type = QualType::getFromOpaquePtr(result)->getAs<TagType>();
103 TagDecl *result_tag_decl = result_tag_type->getDecl();
104
105 if (tag_decl)
106 {
107 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
108
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000109 minion_sp->ImportDefinitionTo(result_tag_decl, tag_decl);
Sean Callananbb120042011-12-16 21:06:35 +0000110
111 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
112
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000113 OriginMap::iterator oi = to_context_md->m_origins.find(result_tag_decl);
114
115 if (oi != to_context_md->m_origins.end() &&
116 oi->second.ctx == src_ctx)
117 to_context_md->m_origins.erase(oi);
Sean Callananbb120042011-12-16 21:06:35 +0000118 }
119 }
120
121 return result;
122}
123
Sean Callanan0eed0d42011-12-06 03:41:14 +0000124clang::Decl *
125ClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx,
126 clang::ASTContext *src_ctx,
127 clang::Decl *decl)
128{
129 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
130
131 if (!result)
132 return NULL;
133
134 ClangASTContext::GetCompleteDecl (src_ctx, decl);
135
136 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
137
138 if (minion_sp && isa<TagDecl>(decl))
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000139 minion_sp->ImportDefinitionTo(result, decl);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000140
141 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
142
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000143 OriginMap::iterator oi = to_context_md->m_origins.find(decl);
144
145 if (oi != to_context_md->m_origins.end() &&
146 oi->second.ctx == src_ctx)
147 to_context_md->m_origins.erase(oi);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000148
149 return result;
150}
151
Sean Callanan5b26f272012-02-04 08:49:35 +0000152void
153ClangASTImporter::CompleteDecl (clang::Decl *decl)
154{
155 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
156
157 if (log)
158 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
159 decl->getDeclKindName(),
160 decl);
161
162 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
163 {
164 if (!interface_decl->getDefinition())
165 {
166 interface_decl->startDefinition();
167 CompleteObjCInterfaceDecl(interface_decl);
168 }
169 }
Greg Clayton23f59502012-07-17 03:23:13 +0000170 else if (ObjCProtocolDecl *protocol_decl = dyn_cast<ObjCProtocolDecl>(decl))
Sean Callanan5b26f272012-02-04 08:49:35 +0000171 {
172 if (!protocol_decl->getDefinition())
173 protocol_decl->startDefinition();
174 }
175 else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
176 {
177 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined())
178 {
179 tag_decl->startDefinition();
180 CompleteTagDecl(tag_decl);
181 tag_decl->setCompleteDefinition(true);
182 }
183 }
Greg Clayton219cf312012-03-30 00:51:13 +0000184 else
185 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000186 assert (0 && "CompleteDecl called on a Decl that can't be completed");
187 }
188}
189
Sean Callanan12014a02011-12-08 23:45:45 +0000190bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000191ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Sean Callanan12014a02011-12-08 23:45:45 +0000192{
Sean Callanancc427fa2011-07-30 02:42:06 +0000193 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +0000194
Sean Callanancc427fa2011-07-30 02:42:06 +0000195 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000196 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000197
Sean Callanancc427fa2011-07-30 02:42:06 +0000198 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000199 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000200
Sean Callanan686b2312011-11-16 18:20:47 +0000201 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
Greg Claytona2721472011-06-25 00:44:06 +0000202
Greg Claytondd0649b2011-07-06 18:55:08 +0000203 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000204 minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
205
Sean Callanan12014a02011-12-08 23:45:45 +0000206 return true;
Greg Claytona2721472011-06-25 00:44:06 +0000207}
Sean Callanancc427fa2011-07-30 02:42:06 +0000208
Sean Callanan12014a02011-12-08 23:45:45 +0000209bool
210ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
211{
212 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
213
214 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
215 return false;
216
217 MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
218
219 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000220 minion_sp->ImportDefinitionTo(decl, origin_decl);
221
Sean Callanan12014a02011-12-08 23:45:45 +0000222 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
223
224 OriginMap &origins = context_md->m_origins;
225
226 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
227
228 return true;
229}
230
231bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000232ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
233{
234 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
235
Sean Callanancc427fa2011-07-30 02:42:06 +0000236 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
237
238 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000239 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000240
241 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000242 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000243
Sean Callanan686b2312011-11-16 18:20:47 +0000244 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000245
246 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000247 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
248
Sean Callanan12014a02011-12-08 23:45:45 +0000249 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000250}
251
Jim Ingham379397632012-10-27 02:54:13 +0000252ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +0000253ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
254{
255 DeclOrigin decl_origin = GetDeclOrigin(decl);
256
257 if (decl_origin.Valid())
258 return ClangASTContext::GetMetadata(decl_origin.ctx, (uintptr_t)decl_origin.decl);
259 else
260 return ClangASTContext::GetMetadata(&decl->getASTContext(), (uintptr_t)decl);
261}
262
Sean Callananf487bd82011-11-16 21:40:57 +0000263ClangASTImporter::DeclOrigin
264ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
265{
266 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
267
268 OriginMap &origins = context_md->m_origins;
269
270 OriginMap::iterator iter = origins.find(decl);
271
272 if (iter != origins.end())
273 return iter->second;
274 else
275 return DeclOrigin();
276}
277
Sean Callanan2cb5e522012-09-20 23:21:16 +0000278void
279ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
280{
281 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
282
283 OriginMap &origins = context_md->m_origins;
284
285 OriginMap::iterator iter = origins.find(decl);
286
287 if (iter != origins.end())
288 {
289 iter->second.decl = original_decl;
290 iter->second.ctx = &original_decl->getASTContext();
291 }
292 else
293 {
294 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
295 }
296}
297
298void
Sean Callanan503aa522011-10-12 00:12:34 +0000299ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
300 NamespaceMapSP &namespace_map)
301{
Sean Callananf487bd82011-11-16 21:40:57 +0000302 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
303
304 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000305}
306
307ClangASTImporter::NamespaceMapSP
308ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
309{
Sean Callananf487bd82011-11-16 21:40:57 +0000310 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
311
312 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000313
Sean Callananf487bd82011-11-16 21:40:57 +0000314 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
315
316 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000317 return iter->second;
318 else
319 return NamespaceMapSP();
320}
321
Sean Callananb2269162011-10-21 22:18:07 +0000322void
323ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
324{
Jim Ingham28eb5712012-10-12 17:34:26 +0000325 assert (decl);
Sean Callananf487bd82011-11-16 21:40:57 +0000326 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
327
Sean Callananb2269162011-10-21 22:18:07 +0000328 const DeclContext *parent_context = decl->getDeclContext();
329 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
330 NamespaceMapSP parent_map;
331
332 if (parent_namespace)
333 parent_map = GetNamespaceMap(parent_namespace);
334
335 NamespaceMapSP new_map;
336
337 new_map.reset(new NamespaceMap);
338
Sean Callananf487bd82011-11-16 21:40:57 +0000339 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000340 {
341 std::string namespace_string = decl->getDeclName().getAsString();
342
Sean Callananf487bd82011-11-16 21:40:57 +0000343 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000344 }
345
Sean Callanan0eed0d42011-12-06 03:41:14 +0000346 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000347}
348
Sean Callanan686b2312011-11-16 18:20:47 +0000349void
Sean Callanan99732312011-11-29 00:42:02 +0000350ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000351{
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000352 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
353
354 if (log)
355 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p", dst_ast);
356
Sean Callananf487bd82011-11-16 21:40:57 +0000357 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000358}
359
Sean Callanan99732312011-11-29 00:42:02 +0000360void
361ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
362{
363 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
364
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000365 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
366
367 if (log)
368 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p", src_ast, dst_ast);
369
Sean Callanan99732312011-11-29 00:42:02 +0000370 if (!md)
371 return;
372
373 md->m_minions.erase(src_ast);
374
375 for (OriginMap::iterator iter = md->m_origins.begin();
376 iter != md->m_origins.end();
377 )
378 {
379 if (iter->second.ctx == src_ast)
380 md->m_origins.erase(iter++);
381 else
382 ++iter;
383 }
384}
385
Sean Callanan0eed0d42011-12-06 03:41:14 +0000386ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000387{
388 return;
389}
390
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000391void
392ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
393{
394 ASTImporter::Imported(from, to);
Sean Callanan5b26f272012-02-04 08:49:35 +0000395
396 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
397
398 /*
399 if (to_objc_interface)
400 to_objc_interface->startDefinition();
401
402 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000403
Sean Callanan5b26f272012-02-04 08:49:35 +0000404 if (to_cxx_record)
405 to_cxx_record->startDefinition();
406 */
407
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000408 ImportDefinition(from);
Sean Callanan5b26f272012-02-04 08:49:35 +0000409
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000410 // If we're dealing with an Objective-C class, ensure that the inheritance has
411 // been set up correctly. The ASTImporter may not do this correctly if the
412 // class was originally sourced from symbols.
413
Sean Callanan5b26f272012-02-04 08:49:35 +0000414 if (to_objc_interface)
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000415 {
416 do
417 {
418 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
419
420 if (to_superclass)
421 break; // we're not going to override it if it's set
422
423 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
424
425 if (!from_objc_interface)
426 break;
427
428 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
429
430 if (!from_superclass)
431 break;
432
433 Decl *imported_from_superclass_decl = Import(from_superclass);
434
435 if (!imported_from_superclass_decl)
436 break;
437
438 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
439
440 if (!imported_from_superclass)
441 break;
442
Sean Callanan5b26f272012-02-04 08:49:35 +0000443 if (!to_objc_interface->hasDefinition())
444 to_objc_interface->startDefinition();
445
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000446 to_objc_interface->setSuperClass(imported_from_superclass);
447 }
448 while (0);
449 }
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000450}
451
Sean Callanancc427fa2011-07-30 02:42:06 +0000452clang::Decl
453*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
454{
455 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000456
457 if (log)
458 {
Jim Ingham379397632012-10-27 02:54:13 +0000459 lldb::user_id_t user_id;
460 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
461 if (metadata)
462 user_id = metadata->GetUserID();
463
Sean Callanan00f43622011-11-18 03:28:09 +0000464 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
465 {
Sean Callanan48e894b2012-05-25 18:12:26 +0000466 std::string name_string;
467 llvm::raw_string_ostream name_stream(name_string);
468 from_named_decl->printName(name_stream);
469 name_stream.flush();
470
Daniel Malead01b2952012-11-29 21:49:15 +0000471 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000472 from->getDeclKindName(),
473 to,
Sean Callanan48e894b2012-05-25 18:12:26 +0000474 name_string.c_str(),
Sean Callanan60217122012-04-13 00:10:03 +0000475 from,
Jim Ingham379397632012-10-27 02:54:13 +0000476 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000477 }
478 else
479 {
Daniel Malead01b2952012-11-29 21:49:15 +0000480 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000481 from->getDeclKindName(),
482 to,
Sean Callanan60217122012-04-13 00:10:03 +0000483 from,
Jim Ingham379397632012-10-27 02:54:13 +0000484 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000485 }
486 }
487
Sean Callananb0b87a52011-11-16 22:23:28 +0000488 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
489 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000490
Sean Callananb0b87a52011-11-16 22:23:28 +0000491 if (from_context_md)
492 {
493 OriginMap &origins = from_context_md->m_origins;
494
495 OriginMap::iterator origin_iter = origins.find(from);
496
497 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000498 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000499 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000500
Sean Callanan5bc5a762011-12-20 23:55:47 +0000501 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
502
503 if (direct_completer.get() != this)
504 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
505
Sean Callanan00f43622011-11-18 03:28:09 +0000506 if (log)
507 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
508 origin_iter->second.decl,
509 origin_iter->second.ctx,
510 &from->getASTContext(),
511 &to->getASTContext());
512 }
513 else
514 {
Sean Callanan0eed0d42011-12-06 03:41:14 +0000515 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
516
Sean Callanan00f43622011-11-18 03:28:09 +0000517 if (log)
518 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
519 &from->getASTContext());
520 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000521
522 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
523 {
524 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
525
526 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
527
528 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
529
530 if (namespace_map_iter != namespace_maps.end())
531 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
532 }
533 }
534 else
535 {
536 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000537
538 if (log)
539 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
540 from,
541 m_source_ctx,
542 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000543 }
544
Sean Callanancc427fa2011-07-30 02:42:06 +0000545 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
546 {
547 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
548
549 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan3d654b32012-09-24 22:25:51 +0000550 to_tag_decl->setMustBuildLookupTable();
551
Sean Callanancc427fa2011-07-30 02:42:06 +0000552 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000553 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000554 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000555 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
556 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
557 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000558
559 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000560 }
561
Sean Callananb2269162011-10-21 22:18:07 +0000562 if (isa<NamespaceDecl>(from))
563 {
564 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
565
566 m_master.BuildNamespaceMap(to_namespace_decl);
567
568 to_namespace_decl->setHasExternalVisibleStorage();
569 }
570
Sean Callanan00f43622011-11-18 03:28:09 +0000571 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000572 {
573 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
Sean Callanana9bc0652012-01-19 02:17:40 +0000574
Sean Callanand5c17ed2011-11-15 02:11:17 +0000575 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000576 to_interface_decl->setHasExternalVisibleStorage();
Sean Callanan5b26f272012-02-04 08:49:35 +0000577
578 /*to_interface_decl->setExternallyCompleted();*/
Sean Callanan0eed0d42011-12-06 03:41:14 +0000579
Sean Callanand5c17ed2011-11-15 02:11:17 +0000580 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000581 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000582 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
583 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
Sean Callanan5b26f272012-02-04 08:49:35 +0000584 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000585 }
586
587 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000588}