blob: a85485247c9412755992b3df977f3111862c6a57 [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
182ClangASTImporter::PurgeMaps (clang::ASTContext *dst_ast)
183{
Sean Callananf487bd82011-11-16 21:40:57 +0000184 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000185}
186
Sean Callananb2269162011-10-21 22:18:07 +0000187ClangASTImporter::NamespaceMapCompleter::~NamespaceMapCompleter ()
188{
189 return;
190}
191
Sean Callanancc427fa2011-07-30 02:42:06 +0000192clang::Decl
193*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
194{
195 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000196
197 if (log)
198 {
199 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
200 {
201 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p)",
202 from->getDeclKindName(),
203 to,
204 from_named_decl->getName().str().c_str(),
205 from);
206 }
207 else
208 {
209 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p)",
210 from->getDeclKindName(),
211 to,
212 from);
213 }
214 }
215
Sean Callananb0b87a52011-11-16 22:23:28 +0000216 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
217 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000218
Sean Callananb0b87a52011-11-16 22:23:28 +0000219 if (from_context_md)
220 {
221 OriginMap &origins = from_context_md->m_origins;
222
223 OriginMap::iterator origin_iter = origins.find(from);
224
225 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000226 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000227 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000228
229 if (log)
230 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
231 origin_iter->second.decl,
232 origin_iter->second.ctx,
233 &from->getASTContext(),
234 &to->getASTContext());
235 }
236 else
237 {
238 if (log)
239 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
240 &from->getASTContext());
241 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000242
243 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
244 {
245 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
246
247 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
248
249 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
250
251 if (namespace_map_iter != namespace_maps.end())
252 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
253 }
254 }
255 else
256 {
257 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000258
259 if (log)
260 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
261 from,
262 m_source_ctx,
263 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000264 }
265
Sean Callanancc427fa2011-07-30 02:42:06 +0000266 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
267 {
268 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
269
270 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000271
Sean Callanancc427fa2011-07-30 02:42:06 +0000272 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000273 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000274 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000275 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
276 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
277 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000278
279 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000280 }
281
Sean Callananb2269162011-10-21 22:18:07 +0000282 if (isa<NamespaceDecl>(from))
283 {
284 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
285
286 m_master.BuildNamespaceMap(to_namespace_decl);
287
288 to_namespace_decl->setHasExternalVisibleStorage();
289 }
290
Sean Callanan00f43622011-11-18 03:28:09 +0000291 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000292 {
293 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
294
Sean Callanand5c17ed2011-11-15 02:11:17 +0000295 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000296 to_interface_decl->setHasExternalVisibleStorage();
297
Sean Callanand5c17ed2011-11-15 02:11:17 +0000298 if (to_interface_decl->isForwardDecl())
299 to_interface_decl->completedForwardDecl();
300
301 to_interface_decl->setExternallyCompleted();
302
303 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000304 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000305 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
306 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
307 (to_interface_decl->isForwardDecl() ? " Forward" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000308 }
309
310 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000311}