blob: 2c7de73914552b239bf9a309e1d8d0c98aee510d [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
Sean Callanan8106d802013-03-08 20:04:57 +000024ClangASTMetrics::Counters ClangASTMetrics::global_counters = { 0, 0, 0, 0, 0, 0 };
25ClangASTMetrics::Counters ClangASTMetrics::local_counters = { 0, 0, 0, 0, 0, 0 };
26
27void ClangASTMetrics::DumpCounters (lldb::LogSP log, ClangASTMetrics::Counters &counters)
28{
Matt Kopec787d1622013-03-12 17:45:38 +000029 log->Printf(" Number of visible Decl queries by name : %" PRIu64, counters.m_visible_query_count);
30 log->Printf(" Number of lexical Decl queries : %" PRIu64, counters.m_lexical_query_count);
31 log->Printf(" Number of imports initiated by LLDB : %" PRIu64, counters.m_lldb_import_count);
32 log->Printf(" Number of imports conducted by Clang : %" PRIu64, counters.m_clang_import_count);
33 log->Printf(" Number of Decls completed : %" PRIu64, counters.m_decls_completed_count);
34 log->Printf(" Number of records laid out : %" PRIu64, counters.m_record_layout_count);
Sean Callanan8106d802013-03-08 20:04:57 +000035}
36
37void ClangASTMetrics::DumpCounters (lldb::LogSP log)
38{
39 if (!log)
40 return;
41
42 log->Printf("== ClangASTMetrics output ==");
43 log->Printf("-- Global metrics --");
44 DumpCounters (log, global_counters);
45 log->Printf("-- Local metrics --");
46 DumpCounters (log, local_counters);
47}
48
49clang::QualType
Sean Callanan686b2312011-11-16 18:20:47 +000050ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
51 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000052 clang::QualType type)
53{
Sean Callanan686b2312011-11-16 18:20:47 +000054 MinionSP minion_sp (GetMinion(dst_ast, src_ast));
Sean Callanancc427fa2011-07-30 02:42:06 +000055
Greg Claytondd0649b2011-07-06 18:55:08 +000056 if (minion_sp)
57 return minion_sp->Import(type);
Sean Callanancc427fa2011-07-30 02:42:06 +000058
Greg Claytondd0649b2011-07-06 18:55:08 +000059 return QualType();
Greg Claytona2721472011-06-25 00:44:06 +000060}
61
Sean Callanan80f78672011-11-16 19:07:39 +000062lldb::clang_type_t
63ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
64 clang::ASTContext *src_ast,
65 lldb::clang_type_t type)
66{
67 return CopyType (dst_ast, src_ast, QualType::getFromOpaquePtr(type)).getAsOpaquePtr();
68}
69
Greg Claytona2721472011-06-25 00:44:06 +000070clang::Decl *
Sean Callanan686b2312011-11-16 18:20:47 +000071ClangASTImporter::CopyDecl (clang::ASTContext *dst_ast,
72 clang::ASTContext *src_ast,
Greg Claytona2721472011-06-25 00:44:06 +000073 clang::Decl *decl)
74{
Greg Claytondd0649b2011-07-06 18:55:08 +000075 MinionSP minion_sp;
Greg Claytona2721472011-06-25 00:44:06 +000076
Sean Callanan686b2312011-11-16 18:20:47 +000077 minion_sp = GetMinion(dst_ast, src_ast);
Greg Claytona2721472011-06-25 00:44:06 +000078
Greg Claytondd0649b2011-07-06 18:55:08 +000079 if (minion_sp)
Sean Callananbfb237bc2011-11-04 22:46:46 +000080 {
81 clang::Decl *result = minion_sp->Import(decl);
82
83 if (!result)
84 {
85 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
86
Sean Callanand5145b32011-11-05 00:08:12 +000087 if (log)
88 {
Jim Ingham379397632012-10-27 02:54:13 +000089 lldb::user_id_t user_id;
90 ClangASTMetadata *metadata = GetDeclMetadata(decl);
91 if (metadata)
92 user_id = metadata->GetUserID();
93
Sean Callanand5145b32011-11-05 00:08:12 +000094 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
Daniel Malead01b2952012-11-29 21:49:15 +000095 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s', metadata 0x%" PRIx64,
Sean Callanand9804fb2012-04-17 22:30:04 +000096 decl->getDeclKindName(),
97 named_decl->getNameAsString().c_str(),
Jim Ingham379397632012-10-27 02:54:13 +000098 user_id);
Sean Callanand5145b32011-11-05 00:08:12 +000099 else
Daniel Malead01b2952012-11-29 21:49:15 +0000100 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s, metadata 0x%" PRIx64,
Sean Callanand9804fb2012-04-17 22:30:04 +0000101 decl->getDeclKindName(),
Jim Ingham379397632012-10-27 02:54:13 +0000102 user_id);
Sean Callanand5145b32011-11-05 00:08:12 +0000103 }
Sean Callananbfb237bc2011-11-04 22:46:46 +0000104 }
105
106 return result;
107 }
Sean Callanancc427fa2011-07-30 02:42:06 +0000108
Greg Claytondd0649b2011-07-06 18:55:08 +0000109 return NULL;
Greg Claytona2721472011-06-25 00:44:06 +0000110}
111
Sean Callananbb120042011-12-16 21:06:35 +0000112lldb::clang_type_t
113ClangASTImporter::DeportType (clang::ASTContext *dst_ctx,
114 clang::ASTContext *src_ctx,
115 lldb::clang_type_t type)
116{
117 lldb::clang_type_t result = CopyType(dst_ctx, src_ctx, type);
118
119 if (!result)
120 return NULL;
121
122 QualType qual_type = QualType::getFromOpaquePtr(type);
123
124 if (const TagType *tag_type = qual_type->getAs<TagType>())
125 {
126 TagDecl *tag_decl = tag_type->getDecl();
127 const TagType *result_tag_type = QualType::getFromOpaquePtr(result)->getAs<TagType>();
128 TagDecl *result_tag_decl = result_tag_type->getDecl();
129
130 if (tag_decl)
131 {
132 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
133
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000134 minion_sp->ImportDefinitionTo(result_tag_decl, tag_decl);
Sean Callananbb120042011-12-16 21:06:35 +0000135
136 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
137
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000138 OriginMap::iterator oi = to_context_md->m_origins.find(result_tag_decl);
139
140 if (oi != to_context_md->m_origins.end() &&
141 oi->second.ctx == src_ctx)
142 to_context_md->m_origins.erase(oi);
Sean Callananbb120042011-12-16 21:06:35 +0000143 }
144 }
145
146 return result;
147}
148
Sean Callanan0eed0d42011-12-06 03:41:14 +0000149clang::Decl *
150ClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx,
151 clang::ASTContext *src_ctx,
152 clang::Decl *decl)
153{
Sean Callanan933ca2e2013-02-28 03:12:58 +0000154 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
155
156 if (log)
157 log->Printf(" [ClangASTImporter] DeportDecl called on (%sDecl*)%p from (ASTContext*)%p to (ASTContex*)%p",
158 decl->getDeclKindName(),
159 decl,
160 src_ctx,
161 dst_ctx);
162
Sean Callanan0eed0d42011-12-06 03:41:14 +0000163 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
164
165 if (!result)
166 return NULL;
Sean Callanan933ca2e2013-02-28 03:12:58 +0000167
Sean Callanan0eed0d42011-12-06 03:41:14 +0000168 ClangASTContext::GetCompleteDecl (src_ctx, decl);
169
170 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
171
172 if (minion_sp && isa<TagDecl>(decl))
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000173 minion_sp->ImportDefinitionTo(result, decl);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000174
175 ASTContextMetadataSP to_context_md = GetContextMetadata(dst_ctx);
176
Sean Callanan933ca2e2013-02-28 03:12:58 +0000177 OriginMap::iterator oi = to_context_md->m_origins.find(result);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000178
179 if (oi != to_context_md->m_origins.end() &&
180 oi->second.ctx == src_ctx)
181 to_context_md->m_origins.erase(oi);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000182
Sean Callanan933ca2e2013-02-28 03:12:58 +0000183 if (TagDecl *result_tag_decl = dyn_cast<TagDecl>(result))
184 {
185 result_tag_decl->setHasExternalLexicalStorage(false);
186 result_tag_decl->setHasExternalVisibleStorage(false);
187 }
188
189 if (log)
190 log->Printf(" [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p",
191 decl->getDeclKindName(),
192 decl,
193 result->getDeclKindName(),
194 result);
195
Sean Callanan0eed0d42011-12-06 03:41:14 +0000196 return result;
197}
198
Sean Callanan5b26f272012-02-04 08:49:35 +0000199void
200ClangASTImporter::CompleteDecl (clang::Decl *decl)
201{
202 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
203
204 if (log)
205 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
206 decl->getDeclKindName(),
207 decl);
208
209 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
210 {
211 if (!interface_decl->getDefinition())
212 {
213 interface_decl->startDefinition();
214 CompleteObjCInterfaceDecl(interface_decl);
215 }
216 }
Greg Clayton23f59502012-07-17 03:23:13 +0000217 else if (ObjCProtocolDecl *protocol_decl = dyn_cast<ObjCProtocolDecl>(decl))
Sean Callanan5b26f272012-02-04 08:49:35 +0000218 {
219 if (!protocol_decl->getDefinition())
220 protocol_decl->startDefinition();
221 }
222 else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
223 {
224 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined())
225 {
226 tag_decl->startDefinition();
227 CompleteTagDecl(tag_decl);
228 tag_decl->setCompleteDefinition(true);
229 }
230 }
Greg Clayton219cf312012-03-30 00:51:13 +0000231 else
232 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000233 assert (0 && "CompleteDecl called on a Decl that can't be completed");
234 }
235}
236
Sean Callanan12014a02011-12-08 23:45:45 +0000237bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000238ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Sean Callanan8106d802013-03-08 20:04:57 +0000239{
240 ClangASTMetrics::RegisterDeclCompletion();
241
Sean Callanancc427fa2011-07-30 02:42:06 +0000242 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +0000243
Sean Callanancc427fa2011-07-30 02:42:06 +0000244 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000245 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000246
Sean Callanancc427fa2011-07-30 02:42:06 +0000247 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000248 return false;
Greg Claytona2721472011-06-25 00:44:06 +0000249
Sean Callanan686b2312011-11-16 18:20:47 +0000250 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
Greg Claytona2721472011-06-25 00:44:06 +0000251
Greg Claytondd0649b2011-07-06 18:55:08 +0000252 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000253 minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
254
Sean Callanan12014a02011-12-08 23:45:45 +0000255 return true;
Greg Claytona2721472011-06-25 00:44:06 +0000256}
Sean Callanancc427fa2011-07-30 02:42:06 +0000257
Sean Callanan12014a02011-12-08 23:45:45 +0000258bool
259ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
260{
Sean Callanan8106d802013-03-08 20:04:57 +0000261 ClangASTMetrics::RegisterDeclCompletion();
262
Sean Callanan12014a02011-12-08 23:45:45 +0000263 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
264
265 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
266 return false;
267
268 MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
269
270 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000271 minion_sp->ImportDefinitionTo(decl, origin_decl);
272
Sean Callanan12014a02011-12-08 23:45:45 +0000273 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
274
275 OriginMap &origins = context_md->m_origins;
276
277 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
278
279 return true;
280}
281
282bool
Sean Callanancc427fa2011-07-30 02:42:06 +0000283ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
284{
Sean Callanan8106d802013-03-08 20:04:57 +0000285 ClangASTMetrics::RegisterDeclCompletion();
286
Sean Callanancc427fa2011-07-30 02:42:06 +0000287 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
288
Sean Callanancc427fa2011-07-30 02:42:06 +0000289 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
290
291 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000292 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000293
294 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000295 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000296
Sean Callanan686b2312011-11-16 18:20:47 +0000297 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000298
299 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000300 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
301
Sean Callanan12014a02011-12-08 23:45:45 +0000302 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000303}
304
Jim Ingham379397632012-10-27 02:54:13 +0000305ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +0000306ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
307{
308 DeclOrigin decl_origin = GetDeclOrigin(decl);
309
310 if (decl_origin.Valid())
311 return ClangASTContext::GetMetadata(decl_origin.ctx, (uintptr_t)decl_origin.decl);
312 else
313 return ClangASTContext::GetMetadata(&decl->getASTContext(), (uintptr_t)decl);
314}
315
Sean Callananf487bd82011-11-16 21:40:57 +0000316ClangASTImporter::DeclOrigin
317ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
318{
319 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
320
321 OriginMap &origins = context_md->m_origins;
322
323 OriginMap::iterator iter = origins.find(decl);
324
325 if (iter != origins.end())
326 return iter->second;
327 else
328 return DeclOrigin();
329}
330
Sean Callanan2cb5e522012-09-20 23:21:16 +0000331void
332ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
333{
334 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
335
336 OriginMap &origins = context_md->m_origins;
337
338 OriginMap::iterator iter = origins.find(decl);
339
340 if (iter != origins.end())
341 {
342 iter->second.decl = original_decl;
343 iter->second.ctx = &original_decl->getASTContext();
344 }
345 else
346 {
347 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
348 }
349}
350
351void
Sean Callanan503aa522011-10-12 00:12:34 +0000352ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
353 NamespaceMapSP &namespace_map)
354{
Sean Callananf487bd82011-11-16 21:40:57 +0000355 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
356
357 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000358}
359
360ClangASTImporter::NamespaceMapSP
361ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
362{
Sean Callananf487bd82011-11-16 21:40:57 +0000363 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
364
365 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000366
Sean Callananf487bd82011-11-16 21:40:57 +0000367 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
368
369 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000370 return iter->second;
371 else
372 return NamespaceMapSP();
373}
374
Sean Callananb2269162011-10-21 22:18:07 +0000375void
376ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
377{
Jim Ingham28eb5712012-10-12 17:34:26 +0000378 assert (decl);
Sean Callananf487bd82011-11-16 21:40:57 +0000379 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
380
Sean Callananb2269162011-10-21 22:18:07 +0000381 const DeclContext *parent_context = decl->getDeclContext();
382 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
383 NamespaceMapSP parent_map;
384
385 if (parent_namespace)
386 parent_map = GetNamespaceMap(parent_namespace);
387
388 NamespaceMapSP new_map;
389
390 new_map.reset(new NamespaceMap);
391
Sean Callananf487bd82011-11-16 21:40:57 +0000392 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000393 {
394 std::string namespace_string = decl->getDeclName().getAsString();
395
Sean Callananf487bd82011-11-16 21:40:57 +0000396 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000397 }
398
Sean Callanan0eed0d42011-12-06 03:41:14 +0000399 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000400}
401
Sean Callanan686b2312011-11-16 18:20:47 +0000402void
Sean Callanan99732312011-11-29 00:42:02 +0000403ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000404{
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000405 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
406
407 if (log)
408 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p", dst_ast);
409
Sean Callananf487bd82011-11-16 21:40:57 +0000410 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000411}
412
Sean Callanan99732312011-11-29 00:42:02 +0000413void
414ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
415{
416 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
417
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000418 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
419
420 if (log)
421 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p", src_ast, dst_ast);
422
Sean Callanan99732312011-11-29 00:42:02 +0000423 if (!md)
424 return;
425
426 md->m_minions.erase(src_ast);
427
428 for (OriginMap::iterator iter = md->m_origins.begin();
429 iter != md->m_origins.end();
430 )
431 {
432 if (iter->second.ctx == src_ast)
433 md->m_origins.erase(iter++);
434 else
435 ++iter;
436 }
437}
438
Sean Callanan0eed0d42011-12-06 03:41:14 +0000439ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000440{
441 return;
442}
443
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000444void
445ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
446{
447 ASTImporter::Imported(from, to);
Sean Callanan5b26f272012-02-04 08:49:35 +0000448
449 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
450
451 /*
452 if (to_objc_interface)
453 to_objc_interface->startDefinition();
454
455 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000456
Sean Callanan5b26f272012-02-04 08:49:35 +0000457 if (to_cxx_record)
458 to_cxx_record->startDefinition();
459 */
460
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000461 ImportDefinition(from);
Sean Callanan5b26f272012-02-04 08:49:35 +0000462
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000463 // If we're dealing with an Objective-C class, ensure that the inheritance has
464 // been set up correctly. The ASTImporter may not do this correctly if the
465 // class was originally sourced from symbols.
466
Sean Callanan5b26f272012-02-04 08:49:35 +0000467 if (to_objc_interface)
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000468 {
469 do
470 {
471 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
472
473 if (to_superclass)
474 break; // we're not going to override it if it's set
475
476 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
477
478 if (!from_objc_interface)
479 break;
480
481 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
482
483 if (!from_superclass)
484 break;
485
486 Decl *imported_from_superclass_decl = Import(from_superclass);
487
488 if (!imported_from_superclass_decl)
489 break;
490
491 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
492
493 if (!imported_from_superclass)
494 break;
495
Sean Callanan5b26f272012-02-04 08:49:35 +0000496 if (!to_objc_interface->hasDefinition())
497 to_objc_interface->startDefinition();
498
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000499 to_objc_interface->setSuperClass(imported_from_superclass);
500 }
501 while (0);
502 }
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000503}
504
Sean Callanancc427fa2011-07-30 02:42:06 +0000505clang::Decl
506*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
507{
Sean Callanan8106d802013-03-08 20:04:57 +0000508 ClangASTMetrics::RegisterClangImport();
509
Sean Callanancc427fa2011-07-30 02:42:06 +0000510 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000511
512 if (log)
513 {
Jim Ingham379397632012-10-27 02:54:13 +0000514 lldb::user_id_t user_id;
515 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
516 if (metadata)
517 user_id = metadata->GetUserID();
518
Sean Callanan00f43622011-11-18 03:28:09 +0000519 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
520 {
Sean Callanan48e894b2012-05-25 18:12:26 +0000521 std::string name_string;
522 llvm::raw_string_ostream name_stream(name_string);
523 from_named_decl->printName(name_stream);
524 name_stream.flush();
525
Daniel Malead01b2952012-11-29 21:49:15 +0000526 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000527 from->getDeclKindName(),
528 to,
Sean Callanan48e894b2012-05-25 18:12:26 +0000529 name_string.c_str(),
Sean Callanan60217122012-04-13 00:10:03 +0000530 from,
Jim Ingham379397632012-10-27 02:54:13 +0000531 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000532 }
533 else
534 {
Daniel Malead01b2952012-11-29 21:49:15 +0000535 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000536 from->getDeclKindName(),
537 to,
Sean Callanan60217122012-04-13 00:10:03 +0000538 from,
Jim Ingham379397632012-10-27 02:54:13 +0000539 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000540 }
541 }
542
Sean Callananb0b87a52011-11-16 22:23:28 +0000543 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
544 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000545
Sean Callananb0b87a52011-11-16 22:23:28 +0000546 if (from_context_md)
547 {
548 OriginMap &origins = from_context_md->m_origins;
549
550 OriginMap::iterator origin_iter = origins.find(from);
551
552 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000553 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000554 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000555
Sean Callanan5bc5a762011-12-20 23:55:47 +0000556 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
557
558 if (direct_completer.get() != this)
559 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
560
Sean Callanan00f43622011-11-18 03:28:09 +0000561 if (log)
562 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
563 origin_iter->second.decl,
564 origin_iter->second.ctx,
565 &from->getASTContext(),
566 &to->getASTContext());
567 }
568 else
569 {
Sean Callanan0eed0d42011-12-06 03:41:14 +0000570 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
571
Sean Callanan00f43622011-11-18 03:28:09 +0000572 if (log)
573 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
574 &from->getASTContext());
575 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000576
577 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
578 {
579 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
580
581 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
582
583 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
584
585 if (namespace_map_iter != namespace_maps.end())
586 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
587 }
588 }
589 else
590 {
591 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000592
593 if (log)
594 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
595 from,
596 m_source_ctx,
597 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000598 }
599
Sean Callanancc427fa2011-07-30 02:42:06 +0000600 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
601 {
602 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
603
604 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan3d654b32012-09-24 22:25:51 +0000605 to_tag_decl->setMustBuildLookupTable();
606
Sean Callanancc427fa2011-07-30 02:42:06 +0000607 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000608 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000609 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000610 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
611 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
612 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000613
614 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000615 }
616
Sean Callananb2269162011-10-21 22:18:07 +0000617 if (isa<NamespaceDecl>(from))
618 {
619 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
620
621 m_master.BuildNamespaceMap(to_namespace_decl);
622
623 to_namespace_decl->setHasExternalVisibleStorage();
624 }
625
Sean Callanan00f43622011-11-18 03:28:09 +0000626 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000627 {
628 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
Sean Callanana9bc0652012-01-19 02:17:40 +0000629
Sean Callanand5c17ed2011-11-15 02:11:17 +0000630 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000631 to_interface_decl->setHasExternalVisibleStorage();
Sean Callanan5b26f272012-02-04 08:49:35 +0000632
633 /*to_interface_decl->setExternallyCompleted();*/
Sean Callanan0eed0d42011-12-06 03:41:14 +0000634
Sean Callanand5c17ed2011-11-15 02:11:17 +0000635 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000636 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000637 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
638 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
Sean Callanan5b26f272012-02-04 08:49:35 +0000639 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000640 }
641
642 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000643}