blob: 2cee0aea035ef143a314e9222d7501ae1f6ed7b2 [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 Callanancc427fa2011-07-30 02:42:06 +000011#include "clang/AST/DeclObjC.h"
12#include "lldb/Core/Log.h"
Greg Clayton7c6d7b82011-10-21 23:04:20 +000013#include "lldb/Core/Module.h"
Greg Claytona2721472011-06-25 00:44:06 +000014#include "lldb/Symbol/ClangASTContext.h"
15#include "lldb/Symbol/ClangASTImporter.h"
Sean Callananb2269162011-10-21 22:18:07 +000016#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Claytona2721472011-06-25 00:44:06 +000017
18using namespace lldb_private;
19using namespace clang;
20
21clang::QualType
Sean Callanan686b2312011-11-16 18:20:47 +000022ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
23 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000024 clang::QualType type)
25{
Sean Callanan686b2312011-11-16 18:20:47 +000026 MinionSP minion_sp (GetMinion(dst_ast, src_ast));
Sean Callanancc427fa2011-07-30 02:42:06 +000027
Greg Claytondd0649b2011-07-06 18:55:08 +000028 if (minion_sp)
29 return minion_sp->Import(type);
Sean Callanancc427fa2011-07-30 02:42:06 +000030
Greg Claytondd0649b2011-07-06 18:55:08 +000031 return QualType();
Greg Claytona2721472011-06-25 00:44:06 +000032}
33
Sean Callanan80f78672011-11-16 19:07:39 +000034lldb::clang_type_t
35ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
36 clang::ASTContext *src_ast,
37 lldb::clang_type_t type)
38{
39 return CopyType (dst_ast, src_ast, QualType::getFromOpaquePtr(type)).getAsOpaquePtr();
40}
41
Greg Claytona2721472011-06-25 00:44:06 +000042clang::Decl *
Sean Callanan686b2312011-11-16 18:20:47 +000043ClangASTImporter::CopyDecl (clang::ASTContext *dst_ast,
44 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000045 clang::Decl *decl)
46{
Greg Claytondd0649b2011-07-06 18:55:08 +000047 MinionSP minion_sp;
Greg Claytona2721472011-06-25 00:44:06 +000048
Sean Callanan686b2312011-11-16 18:20:47 +000049 minion_sp = GetMinion(dst_ast, src_ast);
Greg Claytona2721472011-06-25 00:44:06 +000050
Greg Claytondd0649b2011-07-06 18:55:08 +000051 if (minion_sp)
Sean Callananbfb237bc2011-11-04 22:46:46 +000052 {
53 clang::Decl *result = minion_sp->Import(decl);
54
55 if (!result)
56 {
57 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
58
Sean Callanand5145b32011-11-05 00:08:12 +000059 if (log)
60 {
61 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
62 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s'", decl->getDeclKindName(), named_decl->getNameAsString().c_str());
63 else
64 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s", decl->getDeclKindName());
65 }
Sean Callananbfb237bc2011-11-04 22:46:46 +000066 }
67
68 return result;
69 }
Sean Callanancc427fa2011-07-30 02:42:06 +000070
Greg Claytondd0649b2011-07-06 18:55:08 +000071 return NULL;
Greg Claytona2721472011-06-25 00:44:06 +000072}
73
Sean Callananbb120042011-12-16 21:06:35 +000074lldb::clang_type_t
75ClangASTImporter::DeportType (clang::ASTContext *dst_ctx,
76 clang::ASTContext *src_ctx,
77 lldb::clang_type_t type)
78{
79 lldb::clang_type_t result = CopyType(dst_ctx, src_ctx, type);
80
81 if (!result)
82 return NULL;
83
84 QualType qual_type = QualType::getFromOpaquePtr(type);
85
86 if (const TagType *tag_type = qual_type->getAs<TagType>())
87 {
88 TagDecl *tag_decl = tag_type->getDecl();
89 const TagType *result_tag_type = QualType::getFromOpaquePtr(result)->getAs<TagType>();
90 TagDecl *result_tag_decl = result_tag_type->getDecl();
91
92 if (tag_decl)
93 {
94 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
95
96 minion_sp->ImportDefinition(tag_decl);
97
98 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
99
100 to_context_md->m_origins.erase(result_tag_decl);
101 }
102 }
103
104 return result;
105}
106
Sean Callanan0eed0d42011-12-06 03:41:14 +0000107clang::Decl *
108ClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx,
109 clang::ASTContext *src_ctx,
110 clang::Decl *decl)
111{
112 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
113
114 if (!result)
115 return NULL;
116
117 ClangASTContext::GetCompleteDecl (src_ctx, decl);
118
119 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
120
121 if (minion_sp && isa<TagDecl>(decl))
122 minion_sp->ImportDefinition(decl);
123
124 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
125
126 to_context_md->m_origins.erase(result);
127
128 return result;
129}
130
Sean Callanan12014a02011-12-08 23:45:45 +0000131bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000132ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Sean Callanan12014a02011-12-08 23:45:45 +0000133{
Sean Callanancc427fa2011-07-30 02:42:06 +0000134 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +0000135
Sean Callanancc427fa2011-07-30 02:42:06 +0000136 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000137 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000138
Sean Callanancc427fa2011-07-30 02:42:06 +0000139 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000140 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000141
Sean Callanan686b2312011-11-16 18:20:47 +0000142 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
Greg Claytona2721472011-06-25 00:44:06 +0000143
Greg Claytondd0649b2011-07-06 18:55:08 +0000144 if (minion_sp)
Sean Callanancc427fa2011-07-30 02:42:06 +0000145 minion_sp->ImportDefinition(decl_origin.decl);
Greg Claytona2721472011-06-25 00:44:06 +0000146
Sean Callanan12014a02011-12-08 23:45:45 +0000147 return true;
Greg Claytona2721472011-06-25 00:44:06 +0000148}
Sean Callanancc427fa2011-07-30 02:42:06 +0000149
Sean Callanan12014a02011-12-08 23:45:45 +0000150bool
151ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
152{
153 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
154
155 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
156 return false;
157
158 MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
159
160 if (minion_sp)
161 minion_sp->ImportDefinition(origin_decl);
162
163 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
164
165 OriginMap &origins = context_md->m_origins;
166
167 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
168
169 return true;
170}
171
172bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000173ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
174{
175 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
176
Sean Callanancc427fa2011-07-30 02:42:06 +0000177 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
178
179 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000180 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000181
182 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000183 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000184
Sean Callanan686b2312011-11-16 18:20:47 +0000185 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000186
187 if (minion_sp)
188 minion_sp->ImportDefinition(decl_origin.decl);
189
Sean Callanan12014a02011-12-08 23:45:45 +0000190 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000191}
192
Sean Callananf487bd82011-11-16 21:40:57 +0000193ClangASTImporter::DeclOrigin
194ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
195{
196 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
197
198 OriginMap &origins = context_md->m_origins;
199
200 OriginMap::iterator iter = origins.find(decl);
201
202 if (iter != origins.end())
203 return iter->second;
204 else
205 return DeclOrigin();
206}
207
Sean Callanan503aa522011-10-12 00:12:34 +0000208void
209ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
210 NamespaceMapSP &namespace_map)
211{
Sean Callananf487bd82011-11-16 21:40:57 +0000212 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
213
214 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000215}
216
217ClangASTImporter::NamespaceMapSP
218ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
219{
Sean Callananf487bd82011-11-16 21:40:57 +0000220 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
221
222 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000223
Sean Callananf487bd82011-11-16 21:40:57 +0000224 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
225
226 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000227 return iter->second;
228 else
229 return NamespaceMapSP();
230}
231
Sean Callananb2269162011-10-21 22:18:07 +0000232void
233ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
234{
Sean Callananf487bd82011-11-16 21:40:57 +0000235 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
236
Sean Callananb2269162011-10-21 22:18:07 +0000237 const DeclContext *parent_context = decl->getDeclContext();
238 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
239 NamespaceMapSP parent_map;
240
241 if (parent_namespace)
242 parent_map = GetNamespaceMap(parent_namespace);
243
244 NamespaceMapSP new_map;
245
246 new_map.reset(new NamespaceMap);
247
Sean Callananf487bd82011-11-16 21:40:57 +0000248 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000249 {
250 std::string namespace_string = decl->getDeclName().getAsString();
251
Sean Callananf487bd82011-11-16 21:40:57 +0000252 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000253 }
254
Sean Callanan0eed0d42011-12-06 03:41:14 +0000255 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000256}
257
Sean Callanan686b2312011-11-16 18:20:47 +0000258void
Sean Callanan99732312011-11-29 00:42:02 +0000259ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000260{
Sean Callananf487bd82011-11-16 21:40:57 +0000261 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000262}
263
Sean Callanan99732312011-11-29 00:42:02 +0000264void
265ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
266{
267 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
268
269 if (!md)
270 return;
271
272 md->m_minions.erase(src_ast);
273
274 for (OriginMap::iterator iter = md->m_origins.begin();
275 iter != md->m_origins.end();
276 )
277 {
278 if (iter->second.ctx == src_ast)
279 md->m_origins.erase(iter++);
280 else
281 ++iter;
282 }
283}
284
Sean Callanan0eed0d42011-12-06 03:41:14 +0000285ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000286{
287 return;
288}
289
Sean Callanan0eed0d42011-12-06 03:41:14 +0000290ClangASTImporter::ObjCInterfaceMapSP
291ClangASTImporter::GetObjCInterfaceMap (const clang::ObjCInterfaceDecl *decl)
292{
293 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
294
295 ObjCInterfaceMetaMap &objc_interface_maps = context_md->m_objc_interface_maps;
296
297 ObjCInterfaceMetaMap::iterator iter = objc_interface_maps.find(decl);
298
299 if (iter != objc_interface_maps.end())
300 return iter->second;
301 else
302 return ObjCInterfaceMapSP();
303}
304
305void
306ClangASTImporter::BuildObjCInterfaceMap (const clang::ObjCInterfaceDecl *decl)
307{
308 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
309
310 ObjCInterfaceMapSP new_map(new ObjCInterfaceMap);
311
312 if (context_md->m_map_completer)
313 {
314 std::string namespace_string = decl->getDeclName().getAsString();
315
316 context_md->m_map_completer->CompleteObjCInterfaceMap(new_map, ConstString(namespace_string.c_str()));
317 }
318
319 context_md->m_objc_interface_maps[decl] = new_map;
320}
321
Sean Callanancc427fa2011-07-30 02:42:06 +0000322clang::Decl
323*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
324{
325 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000326
327 if (log)
328 {
329 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
330 {
331 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p)",
332 from->getDeclKindName(),
333 to,
334 from_named_decl->getName().str().c_str(),
335 from);
336 }
337 else
338 {
339 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p)",
340 from->getDeclKindName(),
341 to,
342 from);
343 }
344 }
345
Sean Callananb0b87a52011-11-16 22:23:28 +0000346 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
347 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000348
Sean Callananb0b87a52011-11-16 22:23:28 +0000349 if (from_context_md)
350 {
351 OriginMap &origins = from_context_md->m_origins;
352
353 OriginMap::iterator origin_iter = origins.find(from);
354
355 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000356 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000357 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000358
359 if (log)
360 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
361 origin_iter->second.decl,
362 origin_iter->second.ctx,
363 &from->getASTContext(),
364 &to->getASTContext());
365 }
366 else
367 {
Sean Callanan0eed0d42011-12-06 03:41:14 +0000368 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
369
Sean Callanan00f43622011-11-18 03:28:09 +0000370 if (log)
371 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
372 &from->getASTContext());
373 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000374
375 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
376 {
377 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
378
379 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
380
381 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
382
383 if (namespace_map_iter != namespace_maps.end())
384 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
385 }
386 }
387 else
388 {
389 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000390
391 if (log)
392 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
393 from,
394 m_source_ctx,
395 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000396 }
397
Sean Callanancc427fa2011-07-30 02:42:06 +0000398 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
399 {
400 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
401
402 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000403
Sean Callanancc427fa2011-07-30 02:42:06 +0000404 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000405 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000406 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000407 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
408 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
409 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000410
411 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000412 }
413
Sean Callananb2269162011-10-21 22:18:07 +0000414 if (isa<NamespaceDecl>(from))
415 {
416 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
417
418 m_master.BuildNamespaceMap(to_namespace_decl);
419
420 to_namespace_decl->setHasExternalVisibleStorage();
421 }
422
Sean Callanan00f43622011-11-18 03:28:09 +0000423 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000424 {
425 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
426
Sean Callanan0eed0d42011-12-06 03:41:14 +0000427 m_master.BuildObjCInterfaceMap(to_interface_decl);
428
Sean Callanand5c17ed2011-11-15 02:11:17 +0000429 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000430 to_interface_decl->setHasExternalVisibleStorage();
431
Sean Callanand5c17ed2011-11-15 02:11:17 +0000432 if (to_interface_decl->isForwardDecl())
433 to_interface_decl->completedForwardDecl();
Sean Callananbb120042011-12-16 21:06:35 +0000434
Sean Callanand5c17ed2011-11-15 02:11:17 +0000435 to_interface_decl->setExternallyCompleted();
Sean Callanan0eed0d42011-12-06 03:41:14 +0000436
Sean Callanand5c17ed2011-11-15 02:11:17 +0000437 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000438 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000439 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
440 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
441 (to_interface_decl->isForwardDecl() ? " Forward" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000442 }
443
444 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000445}