blob: 43a75329ff36f08c2056005bd7d1022375159c62 [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 Callanancc427fa2011-07-30 02:42:06 +000074void
75ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Greg Claytona2721472011-06-25 00:44:06 +000076{
Sean Callanancc427fa2011-07-30 02:42:06 +000077 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Claytona2721472011-06-25 00:44:06 +000078
Sean Callanancc427fa2011-07-30 02:42:06 +000079 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +000080
Sean Callanancc427fa2011-07-30 02:42:06 +000081 if (!decl_origin.Valid())
82 return;
Greg Claytona2721472011-06-25 00:44:06 +000083
Sean Callanancc427fa2011-07-30 02:42:06 +000084 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
85 return;
Greg Claytona2721472011-06-25 00:44:06 +000086
Sean Callanan686b2312011-11-16 18:20:47 +000087 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
Greg Claytona2721472011-06-25 00:44:06 +000088
Greg Claytondd0649b2011-07-06 18:55:08 +000089 if (minion_sp)
Sean Callanancc427fa2011-07-30 02:42:06 +000090 minion_sp->ImportDefinition(decl_origin.decl);
Greg Claytona2721472011-06-25 00:44:06 +000091
Sean Callanancc427fa2011-07-30 02:42:06 +000092 return;
Greg Claytona2721472011-06-25 00:44:06 +000093}
Sean Callanancc427fa2011-07-30 02:42:06 +000094
95void
96ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
97{
98 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
99
Sean Callanancc427fa2011-07-30 02:42:06 +0000100 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
101
102 if (!decl_origin.Valid())
103 return;
104
105 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
106 return;
107
Sean Callanan686b2312011-11-16 18:20:47 +0000108 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000109
110 if (minion_sp)
111 minion_sp->ImportDefinition(decl_origin.decl);
112
113 return;
114}
115
Sean Callananf487bd82011-11-16 21:40:57 +0000116ClangASTImporter::DeclOrigin
117ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
118{
119 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
120
121 OriginMap &origins = context_md->m_origins;
122
123 OriginMap::iterator iter = origins.find(decl);
124
125 if (iter != origins.end())
126 return iter->second;
127 else
128 return DeclOrigin();
129}
130
Sean Callanan503aa522011-10-12 00:12:34 +0000131void
132ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
133 NamespaceMapSP &namespace_map)
134{
Sean Callananf487bd82011-11-16 21:40:57 +0000135 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
136
137 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000138}
139
140ClangASTImporter::NamespaceMapSP
141ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
142{
Sean Callananf487bd82011-11-16 21:40:57 +0000143 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
144
145 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000146
Sean Callananf487bd82011-11-16 21:40:57 +0000147 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
148
149 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000150 return iter->second;
151 else
152 return NamespaceMapSP();
153}
154
Sean Callananb2269162011-10-21 22:18:07 +0000155void
156ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
157{
Sean Callananf487bd82011-11-16 21:40:57 +0000158 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
159
Sean Callananb2269162011-10-21 22:18:07 +0000160 const DeclContext *parent_context = decl->getDeclContext();
161 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
162 NamespaceMapSP parent_map;
163
164 if (parent_namespace)
165 parent_map = GetNamespaceMap(parent_namespace);
166
167 NamespaceMapSP new_map;
168
169 new_map.reset(new NamespaceMap);
170
Sean Callananf487bd82011-11-16 21:40:57 +0000171 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000172 {
173 std::string namespace_string = decl->getDeclName().getAsString();
174
Sean Callananf487bd82011-11-16 21:40:57 +0000175 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000176 }
177
178 RegisterNamespaceMap (decl, new_map);
179}
180
Sean Callanan686b2312011-11-16 18:20:47 +0000181void
Sean Callanan99732312011-11-29 00:42:02 +0000182ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000183{
Sean Callananf487bd82011-11-16 21:40:57 +0000184 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000185}
186
Sean Callanan99732312011-11-29 00:42:02 +0000187void
188ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
189{
190 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
191
192 if (!md)
193 return;
194
195 md->m_minions.erase(src_ast);
196
197 for (OriginMap::iterator iter = md->m_origins.begin();
198 iter != md->m_origins.end();
199 )
200 {
201 if (iter->second.ctx == src_ast)
202 md->m_origins.erase(iter++);
203 else
204 ++iter;
205 }
206}
207
Sean Callananb2269162011-10-21 22:18:07 +0000208ClangASTImporter::NamespaceMapCompleter::~NamespaceMapCompleter ()
209{
210 return;
211}
212
Sean Callanancc427fa2011-07-30 02:42:06 +0000213clang::Decl
214*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
215{
216 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000217
218 if (log)
219 {
220 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
221 {
222 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p)",
223 from->getDeclKindName(),
224 to,
225 from_named_decl->getName().str().c_str(),
226 from);
227 }
228 else
229 {
230 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p)",
231 from->getDeclKindName(),
232 to,
233 from);
234 }
235 }
236
Sean Callananb0b87a52011-11-16 22:23:28 +0000237 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
238 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000239
Sean Callananb0b87a52011-11-16 22:23:28 +0000240 if (from_context_md)
241 {
242 OriginMap &origins = from_context_md->m_origins;
243
244 OriginMap::iterator origin_iter = origins.find(from);
245
246 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000247 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000248 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000249
250 if (log)
251 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
252 origin_iter->second.decl,
253 origin_iter->second.ctx,
254 &from->getASTContext(),
255 &to->getASTContext());
256 }
257 else
258 {
259 if (log)
260 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
261 &from->getASTContext());
262 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000263
264 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
265 {
266 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
267
268 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
269
270 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
271
272 if (namespace_map_iter != namespace_maps.end())
273 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
274 }
275 }
276 else
277 {
278 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000279
280 if (log)
281 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
282 from,
283 m_source_ctx,
284 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000285 }
286
Sean Callanancc427fa2011-07-30 02:42:06 +0000287 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
288 {
289 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
290
291 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000292
Sean Callanancc427fa2011-07-30 02:42:06 +0000293 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000294 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000295 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000296 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
297 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
298 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000299
300 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000301 }
302
Sean Callananb2269162011-10-21 22:18:07 +0000303 if (isa<NamespaceDecl>(from))
304 {
305 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
306
307 m_master.BuildNamespaceMap(to_namespace_decl);
308
309 to_namespace_decl->setHasExternalVisibleStorage();
310 }
311
Sean Callanan00f43622011-11-18 03:28:09 +0000312 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000313 {
314 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
315
Sean Callanand5c17ed2011-11-15 02:11:17 +0000316 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000317 to_interface_decl->setHasExternalVisibleStorage();
318
Sean Callanand5c17ed2011-11-15 02:11:17 +0000319 if (to_interface_decl->isForwardDecl())
320 to_interface_decl->completedForwardDecl();
321
322 to_interface_decl->setExternallyCompleted();
323
324 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000325 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000326 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
327 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
328 (to_interface_decl->isForwardDecl() ? " Forward" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000329 }
330
331 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000332}