blob: 0e810cfa2108cfbba47d9e07ee898e6b0b255523 [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{
Sean Callanan933ca2e2013-02-28 03:12:58 +0000129 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
130
131 if (log)
132 log->Printf(" [ClangASTImporter] DeportDecl called on (%sDecl*)%p from (ASTContext*)%p to (ASTContex*)%p",
133 decl->getDeclKindName(),
134 decl,
135 src_ctx,
136 dst_ctx);
137
Sean Callanan0eed0d42011-12-06 03:41:14 +0000138 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
139
140 if (!result)
141 return NULL;
Sean Callanan933ca2e2013-02-28 03:12:58 +0000142
Sean Callanan0eed0d42011-12-06 03:41:14 +0000143 ClangASTContext::GetCompleteDecl (src_ctx, decl);
144
145 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
146
147 if (minion_sp && isa<TagDecl>(decl))
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000148 minion_sp->ImportDefinitionTo(result, decl);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000149
150 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
151
Sean Callanan933ca2e2013-02-28 03:12:58 +0000152 OriginMap::iterator oi = to_context_md->m_origins.find(result);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000153
154 if (oi != to_context_md->m_origins.end() &&
155 oi->second.ctx == src_ctx)
156 to_context_md->m_origins.erase(oi);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000157
Sean Callanan933ca2e2013-02-28 03:12:58 +0000158 if (TagDecl *result_tag_decl = dyn_cast<TagDecl>(result))
159 {
160 result_tag_decl->setHasExternalLexicalStorage(false);
161 result_tag_decl->setHasExternalVisibleStorage(false);
162 }
163
164 if (log)
165 log->Printf(" [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p",
166 decl->getDeclKindName(),
167 decl,
168 result->getDeclKindName(),
169 result);
170
Sean Callanan0eed0d42011-12-06 03:41:14 +0000171 return result;
172}
173
Sean Callanan5b26f272012-02-04 08:49:35 +0000174void
175ClangASTImporter::CompleteDecl (clang::Decl *decl)
176{
177 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
178
179 if (log)
180 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
181 decl->getDeclKindName(),
182 decl);
183
184 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
185 {
186 if (!interface_decl->getDefinition())
187 {
188 interface_decl->startDefinition();
189 CompleteObjCInterfaceDecl(interface_decl);
190 }
191 }
Greg Clayton23f59502012-07-17 03:23:13 +0000192 else if (ObjCProtocolDecl *protocol_decl = dyn_cast<ObjCProtocolDecl>(decl))
Sean Callanan5b26f272012-02-04 08:49:35 +0000193 {
194 if (!protocol_decl->getDefinition())
195 protocol_decl->startDefinition();
196 }
197 else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
198 {
199 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined())
200 {
201 tag_decl->startDefinition();
202 CompleteTagDecl(tag_decl);
203 tag_decl->setCompleteDefinition(true);
204 }
205 }
Greg Clayton219cf312012-03-30 00:51:13 +0000206 else
207 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000208 assert (0 && "CompleteDecl called on a Decl that can't be completed");
209 }
210}
211
Sean Callanan12014a02011-12-08 23:45:45 +0000212bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000213ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Sean Callanan12014a02011-12-08 23:45:45 +0000214{
Sean Callanancc427fa2011-07-30 02:42:06 +0000215 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +0000216
Sean Callanancc427fa2011-07-30 02:42:06 +0000217 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000218 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000219
Sean Callanancc427fa2011-07-30 02:42:06 +0000220 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000221 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000222
Sean Callanan686b2312011-11-16 18:20:47 +0000223 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
Greg Claytona2721472011-06-25 00:44:06 +0000224
Greg Claytondd0649b2011-07-06 18:55:08 +0000225 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000226 minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
227
Sean Callanan12014a02011-12-08 23:45:45 +0000228 return true;
Greg Claytona2721472011-06-25 00:44:06 +0000229}
Sean Callanancc427fa2011-07-30 02:42:06 +0000230
Sean Callanan12014a02011-12-08 23:45:45 +0000231bool
232ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
233{
234 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
235
236 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
237 return false;
238
239 MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
240
241 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000242 minion_sp->ImportDefinitionTo(decl, origin_decl);
243
Sean Callanan12014a02011-12-08 23:45:45 +0000244 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
245
246 OriginMap &origins = context_md->m_origins;
247
248 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
249
250 return true;
251}
252
253bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000254ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
255{
256 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
257
Sean Callanancc427fa2011-07-30 02:42:06 +0000258 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
259
260 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000261 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000262
263 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000264 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000265
Sean Callanan686b2312011-11-16 18:20:47 +0000266 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000267
268 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000269 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
270
Sean Callanan12014a02011-12-08 23:45:45 +0000271 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000272}
273
Jim Ingham379397632012-10-27 02:54:13 +0000274ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +0000275ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
276{
277 DeclOrigin decl_origin = GetDeclOrigin(decl);
278
279 if (decl_origin.Valid())
280 return ClangASTContext::GetMetadata(decl_origin.ctx, (uintptr_t)decl_origin.decl);
281 else
282 return ClangASTContext::GetMetadata(&decl->getASTContext(), (uintptr_t)decl);
283}
284
Sean Callananf487bd82011-11-16 21:40:57 +0000285ClangASTImporter::DeclOrigin
286ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
287{
288 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
289
290 OriginMap &origins = context_md->m_origins;
291
292 OriginMap::iterator iter = origins.find(decl);
293
294 if (iter != origins.end())
295 return iter->second;
296 else
297 return DeclOrigin();
298}
299
Sean Callanan2cb5e522012-09-20 23:21:16 +0000300void
301ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
302{
303 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
304
305 OriginMap &origins = context_md->m_origins;
306
307 OriginMap::iterator iter = origins.find(decl);
308
309 if (iter != origins.end())
310 {
311 iter->second.decl = original_decl;
312 iter->second.ctx = &original_decl->getASTContext();
313 }
314 else
315 {
316 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
317 }
318}
319
320void
Sean Callanan503aa522011-10-12 00:12:34 +0000321ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
322 NamespaceMapSP &namespace_map)
323{
Sean Callananf487bd82011-11-16 21:40:57 +0000324 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
325
326 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000327}
328
329ClangASTImporter::NamespaceMapSP
330ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
331{
Sean Callananf487bd82011-11-16 21:40:57 +0000332 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
333
334 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000335
Sean Callananf487bd82011-11-16 21:40:57 +0000336 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
337
338 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000339 return iter->second;
340 else
341 return NamespaceMapSP();
342}
343
Sean Callananb2269162011-10-21 22:18:07 +0000344void
345ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
346{
Jim Ingham28eb5712012-10-12 17:34:26 +0000347 assert (decl);
Sean Callananf487bd82011-11-16 21:40:57 +0000348 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
349
Sean Callananb2269162011-10-21 22:18:07 +0000350 const DeclContext *parent_context = decl->getDeclContext();
351 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
352 NamespaceMapSP parent_map;
353
354 if (parent_namespace)
355 parent_map = GetNamespaceMap(parent_namespace);
356
357 NamespaceMapSP new_map;
358
359 new_map.reset(new NamespaceMap);
360
Sean Callananf487bd82011-11-16 21:40:57 +0000361 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000362 {
363 std::string namespace_string = decl->getDeclName().getAsString();
364
Sean Callananf487bd82011-11-16 21:40:57 +0000365 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000366 }
367
Sean Callanan0eed0d42011-12-06 03:41:14 +0000368 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000369}
370
Sean Callanan686b2312011-11-16 18:20:47 +0000371void
Sean Callanan99732312011-11-29 00:42:02 +0000372ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000373{
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000374 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
375
376 if (log)
377 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p", dst_ast);
378
Sean Callananf487bd82011-11-16 21:40:57 +0000379 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000380}
381
Sean Callanan99732312011-11-29 00:42:02 +0000382void
383ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
384{
385 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
386
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000387 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
388
389 if (log)
390 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p", src_ast, dst_ast);
391
Sean Callanan99732312011-11-29 00:42:02 +0000392 if (!md)
393 return;
394
395 md->m_minions.erase(src_ast);
396
397 for (OriginMap::iterator iter = md->m_origins.begin();
398 iter != md->m_origins.end();
399 )
400 {
401 if (iter->second.ctx == src_ast)
402 md->m_origins.erase(iter++);
403 else
404 ++iter;
405 }
406}
407
Sean Callanan0eed0d42011-12-06 03:41:14 +0000408ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000409{
410 return;
411}
412
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000413void
414ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
415{
416 ASTImporter::Imported(from, to);
Sean Callanan5b26f272012-02-04 08:49:35 +0000417
418 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
419
420 /*
421 if (to_objc_interface)
422 to_objc_interface->startDefinition();
423
424 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000425
Sean Callanan5b26f272012-02-04 08:49:35 +0000426 if (to_cxx_record)
427 to_cxx_record->startDefinition();
428 */
429
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000430 ImportDefinition(from);
Sean Callanan5b26f272012-02-04 08:49:35 +0000431
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000432 // If we're dealing with an Objective-C class, ensure that the inheritance has
433 // been set up correctly. The ASTImporter may not do this correctly if the
434 // class was originally sourced from symbols.
435
Sean Callanan5b26f272012-02-04 08:49:35 +0000436 if (to_objc_interface)
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000437 {
438 do
439 {
440 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
441
442 if (to_superclass)
443 break; // we're not going to override it if it's set
444
445 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
446
447 if (!from_objc_interface)
448 break;
449
450 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
451
452 if (!from_superclass)
453 break;
454
455 Decl *imported_from_superclass_decl = Import(from_superclass);
456
457 if (!imported_from_superclass_decl)
458 break;
459
460 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
461
462 if (!imported_from_superclass)
463 break;
464
Sean Callanan5b26f272012-02-04 08:49:35 +0000465 if (!to_objc_interface->hasDefinition())
466 to_objc_interface->startDefinition();
467
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000468 to_objc_interface->setSuperClass(imported_from_superclass);
469 }
470 while (0);
471 }
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000472}
473
Sean Callanancc427fa2011-07-30 02:42:06 +0000474clang::Decl
475*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
476{
477 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000478
479 if (log)
480 {
Jim Ingham379397632012-10-27 02:54:13 +0000481 lldb::user_id_t user_id;
482 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
483 if (metadata)
484 user_id = metadata->GetUserID();
485
Sean Callanan00f43622011-11-18 03:28:09 +0000486 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
487 {
Sean Callanan48e894b2012-05-25 18:12:26 +0000488 std::string name_string;
489 llvm::raw_string_ostream name_stream(name_string);
490 from_named_decl->printName(name_stream);
491 name_stream.flush();
492
Daniel Malead01b2952012-11-29 21:49:15 +0000493 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000494 from->getDeclKindName(),
495 to,
Sean Callanan48e894b2012-05-25 18:12:26 +0000496 name_string.c_str(),
Sean Callanan60217122012-04-13 00:10:03 +0000497 from,
Jim Ingham379397632012-10-27 02:54:13 +0000498 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000499 }
500 else
501 {
Daniel Malead01b2952012-11-29 21:49:15 +0000502 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000503 from->getDeclKindName(),
504 to,
Sean Callanan60217122012-04-13 00:10:03 +0000505 from,
Jim Ingham379397632012-10-27 02:54:13 +0000506 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000507 }
508 }
509
Sean Callananb0b87a52011-11-16 22:23:28 +0000510 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
511 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000512
Sean Callananb0b87a52011-11-16 22:23:28 +0000513 if (from_context_md)
514 {
515 OriginMap &origins = from_context_md->m_origins;
516
517 OriginMap::iterator origin_iter = origins.find(from);
518
519 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000520 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000521 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000522
Sean Callanan5bc5a762011-12-20 23:55:47 +0000523 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
524
525 if (direct_completer.get() != this)
526 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
527
Sean Callanan00f43622011-11-18 03:28:09 +0000528 if (log)
529 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
530 origin_iter->second.decl,
531 origin_iter->second.ctx,
532 &from->getASTContext(),
533 &to->getASTContext());
534 }
535 else
536 {
Sean Callanan0eed0d42011-12-06 03:41:14 +0000537 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
538
Sean Callanan00f43622011-11-18 03:28:09 +0000539 if (log)
540 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
541 &from->getASTContext());
542 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000543
544 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
545 {
546 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
547
548 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
549
550 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
551
552 if (namespace_map_iter != namespace_maps.end())
553 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
554 }
555 }
556 else
557 {
558 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000559
560 if (log)
561 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
562 from,
563 m_source_ctx,
564 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000565 }
566
Sean Callanancc427fa2011-07-30 02:42:06 +0000567 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
568 {
569 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
570
571 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan3d654b32012-09-24 22:25:51 +0000572 to_tag_decl->setMustBuildLookupTable();
573
Sean Callanancc427fa2011-07-30 02:42:06 +0000574 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000575 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000576 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000577 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
578 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
579 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000580
581 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000582 }
583
Sean Callananb2269162011-10-21 22:18:07 +0000584 if (isa<NamespaceDecl>(from))
585 {
586 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
587
588 m_master.BuildNamespaceMap(to_namespace_decl);
589
590 to_namespace_decl->setHasExternalVisibleStorage();
591 }
592
Sean Callanan00f43622011-11-18 03:28:09 +0000593 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000594 {
595 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
Sean Callanana9bc0652012-01-19 02:17:40 +0000596
Sean Callanand5c17ed2011-11-15 02:11:17 +0000597 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000598 to_interface_decl->setHasExternalVisibleStorage();
Sean Callanan5b26f272012-02-04 08:49:35 +0000599
600 /*to_interface_decl->setExternallyCompleted();*/
Sean Callanan0eed0d42011-12-06 03:41:14 +0000601
Sean Callanand5c17ed2011-11-15 02:11:17 +0000602 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000603 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000604 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
605 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
Sean Callanan5b26f272012-02-04 08:49:35 +0000606 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000607 }
608
609 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000610}