blob: 3bbd9c6af35859db8a80ab3d94620534a9208c94 [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 Callanan0eed0d42011-12-06 03:41:14 +000074clang::Decl *
75ClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx,
76 clang::ASTContext *src_ctx,
77 clang::Decl *decl)
78{
79 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
80
81 if (!result)
82 return NULL;
83
84 ClangASTContext::GetCompleteDecl (src_ctx, decl);
85
86 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
87
88 if (minion_sp && isa<TagDecl>(decl))
89 minion_sp->ImportDefinition(decl);
90
91 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
92
93 to_context_md->m_origins.erase(result);
94
95 return result;
96}
97
Sean Callanan12014a02011-12-08 23:45:45 +000098bool
Sean Callanancc427fa2011-07-30 02:42:06 +000099ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Sean Callanan12014a02011-12-08 23:45:45 +0000100{
Sean Callanancc427fa2011-07-30 02:42:06 +0000101 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +0000102
Sean Callanancc427fa2011-07-30 02:42:06 +0000103 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000104 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000105
Sean Callanancc427fa2011-07-30 02:42:06 +0000106 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000107 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000108
Sean Callanan686b2312011-11-16 18:20:47 +0000109 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
Greg Claytona2721472011-06-25 00:44:06 +0000110
Greg Claytondd0649b2011-07-06 18:55:08 +0000111 if (minion_sp)
Sean Callanancc427fa2011-07-30 02:42:06 +0000112 minion_sp->ImportDefinition(decl_origin.decl);
Greg Claytona2721472011-06-25 00:44:06 +0000113
Sean Callanan12014a02011-12-08 23:45:45 +0000114 return true;
Greg Claytona2721472011-06-25 00:44:06 +0000115}
Sean Callanancc427fa2011-07-30 02:42:06 +0000116
Sean Callanan12014a02011-12-08 23:45:45 +0000117bool
118ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
119{
120 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
121
122 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
123 return false;
124
125 MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
126
127 if (minion_sp)
128 minion_sp->ImportDefinition(origin_decl);
129
130 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
131
132 OriginMap &origins = context_md->m_origins;
133
134 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
135
136 return true;
137}
138
139bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000140ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
141{
142 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
143
Sean Callanancc427fa2011-07-30 02:42:06 +0000144 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
145
146 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000147 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000148
149 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000150 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000151
Sean Callanan686b2312011-11-16 18:20:47 +0000152 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000153
154 if (minion_sp)
155 minion_sp->ImportDefinition(decl_origin.decl);
156
Sean Callanan12014a02011-12-08 23:45:45 +0000157 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000158}
159
Sean Callananf487bd82011-11-16 21:40:57 +0000160ClangASTImporter::DeclOrigin
161ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
162{
163 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
164
165 OriginMap &origins = context_md->m_origins;
166
167 OriginMap::iterator iter = origins.find(decl);
168
169 if (iter != origins.end())
170 return iter->second;
171 else
172 return DeclOrigin();
173}
174
Sean Callanan503aa522011-10-12 00:12:34 +0000175void
176ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
177 NamespaceMapSP &namespace_map)
178{
Sean Callananf487bd82011-11-16 21:40:57 +0000179 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
180
181 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000182}
183
184ClangASTImporter::NamespaceMapSP
185ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
186{
Sean Callananf487bd82011-11-16 21:40:57 +0000187 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
188
189 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000190
Sean Callananf487bd82011-11-16 21:40:57 +0000191 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
192
193 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000194 return iter->second;
195 else
196 return NamespaceMapSP();
197}
198
Sean Callananb2269162011-10-21 22:18:07 +0000199void
200ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
201{
Sean Callananf487bd82011-11-16 21:40:57 +0000202 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
203
Sean Callananb2269162011-10-21 22:18:07 +0000204 const DeclContext *parent_context = decl->getDeclContext();
205 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
206 NamespaceMapSP parent_map;
207
208 if (parent_namespace)
209 parent_map = GetNamespaceMap(parent_namespace);
210
211 NamespaceMapSP new_map;
212
213 new_map.reset(new NamespaceMap);
214
Sean Callananf487bd82011-11-16 21:40:57 +0000215 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000216 {
217 std::string namespace_string = decl->getDeclName().getAsString();
218
Sean Callananf487bd82011-11-16 21:40:57 +0000219 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000220 }
221
Sean Callanan0eed0d42011-12-06 03:41:14 +0000222 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000223}
224
Sean Callanan686b2312011-11-16 18:20:47 +0000225void
Sean Callanan99732312011-11-29 00:42:02 +0000226ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000227{
Sean Callananf487bd82011-11-16 21:40:57 +0000228 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000229}
230
Sean Callanan99732312011-11-29 00:42:02 +0000231void
232ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
233{
234 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
235
236 if (!md)
237 return;
238
239 md->m_minions.erase(src_ast);
240
241 for (OriginMap::iterator iter = md->m_origins.begin();
242 iter != md->m_origins.end();
243 )
244 {
245 if (iter->second.ctx == src_ast)
246 md->m_origins.erase(iter++);
247 else
248 ++iter;
249 }
250}
251
Sean Callanan0eed0d42011-12-06 03:41:14 +0000252ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000253{
254 return;
255}
256
Sean Callanan0eed0d42011-12-06 03:41:14 +0000257ClangASTImporter::ObjCInterfaceMapSP
258ClangASTImporter::GetObjCInterfaceMap (const clang::ObjCInterfaceDecl *decl)
259{
260 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
261
262 ObjCInterfaceMetaMap &objc_interface_maps = context_md->m_objc_interface_maps;
263
264 ObjCInterfaceMetaMap::iterator iter = objc_interface_maps.find(decl);
265
266 if (iter != objc_interface_maps.end())
267 return iter->second;
268 else
269 return ObjCInterfaceMapSP();
270}
271
272void
273ClangASTImporter::BuildObjCInterfaceMap (const clang::ObjCInterfaceDecl *decl)
274{
275 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
276
277 ObjCInterfaceMapSP new_map(new ObjCInterfaceMap);
278
279 if (context_md->m_map_completer)
280 {
281 std::string namespace_string = decl->getDeclName().getAsString();
282
283 context_md->m_map_completer->CompleteObjCInterfaceMap(new_map, ConstString(namespace_string.c_str()));
284 }
285
286 context_md->m_objc_interface_maps[decl] = new_map;
287}
288
Sean Callanancc427fa2011-07-30 02:42:06 +0000289clang::Decl
290*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
291{
292 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000293
294 if (log)
295 {
296 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
297 {
298 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p)",
299 from->getDeclKindName(),
300 to,
301 from_named_decl->getName().str().c_str(),
302 from);
303 }
304 else
305 {
306 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p)",
307 from->getDeclKindName(),
308 to,
309 from);
310 }
311 }
312
Sean Callananb0b87a52011-11-16 22:23:28 +0000313 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
314 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000315
Sean Callananb0b87a52011-11-16 22:23:28 +0000316 if (from_context_md)
317 {
318 OriginMap &origins = from_context_md->m_origins;
319
320 OriginMap::iterator origin_iter = origins.find(from);
321
322 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000323 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000324 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000325
326 if (log)
327 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
328 origin_iter->second.decl,
329 origin_iter->second.ctx,
330 &from->getASTContext(),
331 &to->getASTContext());
332 }
333 else
334 {
Sean Callanan0eed0d42011-12-06 03:41:14 +0000335 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
336
Sean Callanan00f43622011-11-18 03:28:09 +0000337 if (log)
338 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
339 &from->getASTContext());
340 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000341
342 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
343 {
344 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
345
346 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
347
348 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
349
350 if (namespace_map_iter != namespace_maps.end())
351 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
352 }
353 }
354 else
355 {
356 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000357
358 if (log)
359 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
360 from,
361 m_source_ctx,
362 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000363 }
364
Sean Callanancc427fa2011-07-30 02:42:06 +0000365 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
366 {
367 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
368
369 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000370
Sean Callanancc427fa2011-07-30 02:42:06 +0000371 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000372 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000373 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000374 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
375 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
376 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000377
378 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000379 }
380
Sean Callananb2269162011-10-21 22:18:07 +0000381 if (isa<NamespaceDecl>(from))
382 {
383 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
384
385 m_master.BuildNamespaceMap(to_namespace_decl);
386
387 to_namespace_decl->setHasExternalVisibleStorage();
388 }
389
Sean Callanan00f43622011-11-18 03:28:09 +0000390 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000391 {
392 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
393
Sean Callanan0eed0d42011-12-06 03:41:14 +0000394 m_master.BuildObjCInterfaceMap(to_interface_decl);
395
Sean Callanand5c17ed2011-11-15 02:11:17 +0000396 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000397 to_interface_decl->setHasExternalVisibleStorage();
398
Sean Callanand5c17ed2011-11-15 02:11:17 +0000399 if (to_interface_decl->isForwardDecl())
400 to_interface_decl->completedForwardDecl();
401
402 to_interface_decl->setExternallyCompleted();
Sean Callanan0eed0d42011-12-06 03:41:14 +0000403
Sean Callanand5c17ed2011-11-15 02:11:17 +0000404 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000405 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000406 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
407 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
408 (to_interface_decl->isForwardDecl() ? " Forward" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000409 }
410
411 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000412}