blob: d3499b993429997aa00b3cd42408073e9b41d536 [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
Greg Clayton5160ce52013-03-27 23:08:40 +000027void ClangASTMetrics::DumpCounters (Log *log, ClangASTMetrics::Counters &counters)
Sean Callanan8106d802013-03-08 20:04:57 +000028{
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
Greg Clayton5160ce52013-03-27 23:08:40 +000037void ClangASTMetrics::DumpCounters (Log *log)
Sean Callanan8106d802013-03-08 20:04:57 +000038{
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 {
Greg Clayton5160ce52013-03-27 23:08:40 +000085 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananbfb237bc2011-11-04 22:46:46 +000086
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{
Greg Clayton5160ce52013-03-27 23:08:40 +0000154 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933ca2e2013-02-28 03:12:58 +0000155
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{
Greg Clayton5160ce52013-03-27 23:08:40 +0000202 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5b26f272012-02-04 08:49:35 +0000203
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();
Sean Callanancc427fa2011-07-30 02:42:06 +0000286
Sean Callanancc427fa2011-07-30 02:42:06 +0000287 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
288
289 if (!decl_origin.Valid())
Sean Callanan12014a02011-12-08 23:45:45 +0000290 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000291
292 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
Sean Callanan12014a02011-12-08 23:45:45 +0000293 return false;
Sean Callanancc427fa2011-07-30 02:42:06 +0000294
Sean Callanan686b2312011-11-16 18:20:47 +0000295 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
Sean Callanancc427fa2011-07-30 02:42:06 +0000296
297 if (minion_sp)
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000298 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
299
Sean Callanan12014a02011-12-08 23:45:45 +0000300 return true;
Sean Callanancc427fa2011-07-30 02:42:06 +0000301}
302
Sean Callanan6b200d02013-03-21 22:15:41 +0000303bool
304ClangASTImporter::RequireCompleteType (clang::QualType type)
305{
306 if (type.isNull())
307 return false;
308
309 if (const TagType *tag_type = type->getAs<TagType>())
310 {
311 return CompleteTagDecl(tag_type->getDecl());
312 }
313 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>())
314 {
315 if (ObjCInterfaceDecl *objc_interface_decl = objc_object_type->getInterface())
316 return CompleteObjCInterfaceDecl(objc_interface_decl);
317 else
318 return false;
319 }
320 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
321 {
322 return RequireCompleteType(array_type->getElementType());
323 }
324 if (const AtomicType *atomic_type = type->getAs<AtomicType>())
325 {
326 return RequireCompleteType(atomic_type->getPointeeType());
327 }
328
329 return true;
330}
331
Jim Ingham379397632012-10-27 02:54:13 +0000332ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +0000333ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
334{
335 DeclOrigin decl_origin = GetDeclOrigin(decl);
336
337 if (decl_origin.Valid())
Greg Claytond0029442013-03-27 01:48:02 +0000338 return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
Sean Callanan60217122012-04-13 00:10:03 +0000339 else
Greg Claytond0029442013-03-27 01:48:02 +0000340 return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
Sean Callanan60217122012-04-13 00:10:03 +0000341}
342
Sean Callananf487bd82011-11-16 21:40:57 +0000343ClangASTImporter::DeclOrigin
344ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
345{
346 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
347
348 OriginMap &origins = context_md->m_origins;
349
350 OriginMap::iterator iter = origins.find(decl);
351
352 if (iter != origins.end())
353 return iter->second;
354 else
355 return DeclOrigin();
356}
357
Sean Callanan2cb5e522012-09-20 23:21:16 +0000358void
359ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
360{
361 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
362
363 OriginMap &origins = context_md->m_origins;
364
365 OriginMap::iterator iter = origins.find(decl);
366
367 if (iter != origins.end())
368 {
369 iter->second.decl = original_decl;
370 iter->second.ctx = &original_decl->getASTContext();
371 }
372 else
373 {
374 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
375 }
376}
377
378void
Sean Callanan503aa522011-10-12 00:12:34 +0000379ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
380 NamespaceMapSP &namespace_map)
381{
Sean Callananf487bd82011-11-16 21:40:57 +0000382 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
383
384 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000385}
386
387ClangASTImporter::NamespaceMapSP
388ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
389{
Sean Callananf487bd82011-11-16 21:40:57 +0000390 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
391
392 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000393
Sean Callananf487bd82011-11-16 21:40:57 +0000394 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
395
396 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000397 return iter->second;
398 else
399 return NamespaceMapSP();
400}
401
Sean Callananb2269162011-10-21 22:18:07 +0000402void
403ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
404{
Jim Ingham28eb5712012-10-12 17:34:26 +0000405 assert (decl);
Sean Callananf487bd82011-11-16 21:40:57 +0000406 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
407
Sean Callananb2269162011-10-21 22:18:07 +0000408 const DeclContext *parent_context = decl->getDeclContext();
409 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
410 NamespaceMapSP parent_map;
411
412 if (parent_namespace)
413 parent_map = GetNamespaceMap(parent_namespace);
414
415 NamespaceMapSP new_map;
416
417 new_map.reset(new NamespaceMap);
418
Sean Callananf487bd82011-11-16 21:40:57 +0000419 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000420 {
421 std::string namespace_string = decl->getDeclName().getAsString();
422
Sean Callananf487bd82011-11-16 21:40:57 +0000423 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000424 }
425
Sean Callanan0eed0d42011-12-06 03:41:14 +0000426 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000427}
428
Sean Callanan686b2312011-11-16 18:20:47 +0000429void
Sean Callanan99732312011-11-29 00:42:02 +0000430ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000431{
Greg Clayton5160ce52013-03-27 23:08:40 +0000432 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000433
434 if (log)
435 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p", dst_ast);
436
Sean Callananf487bd82011-11-16 21:40:57 +0000437 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000438}
439
Sean Callanan99732312011-11-29 00:42:02 +0000440void
441ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
442{
443 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
444
Greg Clayton5160ce52013-03-27 23:08:40 +0000445 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000446
447 if (log)
448 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p", src_ast, dst_ast);
449
Sean Callanan99732312011-11-29 00:42:02 +0000450 if (!md)
451 return;
452
453 md->m_minions.erase(src_ast);
454
455 for (OriginMap::iterator iter = md->m_origins.begin();
456 iter != md->m_origins.end();
457 )
458 {
459 if (iter->second.ctx == src_ast)
460 md->m_origins.erase(iter++);
461 else
462 ++iter;
463 }
464}
465
Sean Callanan0eed0d42011-12-06 03:41:14 +0000466ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000467{
468 return;
469}
470
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000471void
472ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
473{
474 ASTImporter::Imported(from, to);
Sean Callanan5b26f272012-02-04 08:49:35 +0000475
476 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
477
478 /*
479 if (to_objc_interface)
480 to_objc_interface->startDefinition();
481
482 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000483
Sean Callanan5b26f272012-02-04 08:49:35 +0000484 if (to_cxx_record)
485 to_cxx_record->startDefinition();
486 */
487
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000488 ImportDefinition(from);
Sean Callanan5b26f272012-02-04 08:49:35 +0000489
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000490 // If we're dealing with an Objective-C class, ensure that the inheritance has
491 // been set up correctly. The ASTImporter may not do this correctly if the
492 // class was originally sourced from symbols.
493
Sean Callanan5b26f272012-02-04 08:49:35 +0000494 if (to_objc_interface)
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000495 {
496 do
497 {
498 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
499
500 if (to_superclass)
501 break; // we're not going to override it if it's set
502
503 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
504
505 if (!from_objc_interface)
506 break;
507
508 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
509
510 if (!from_superclass)
511 break;
512
513 Decl *imported_from_superclass_decl = Import(from_superclass);
514
515 if (!imported_from_superclass_decl)
516 break;
517
518 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
519
520 if (!imported_from_superclass)
521 break;
522
Sean Callanan5b26f272012-02-04 08:49:35 +0000523 if (!to_objc_interface->hasDefinition())
524 to_objc_interface->startDefinition();
525
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000526 to_objc_interface->setSuperClass(imported_from_superclass);
527 }
528 while (0);
529 }
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000530}
531
Sean Callanancc427fa2011-07-30 02:42:06 +0000532clang::Decl
533*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
534{
Sean Callanan8106d802013-03-08 20:04:57 +0000535 ClangASTMetrics::RegisterClangImport();
536
Greg Clayton5160ce52013-03-27 23:08:40 +0000537 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000538
539 if (log)
540 {
Jim Ingham379397632012-10-27 02:54:13 +0000541 lldb::user_id_t user_id;
542 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
543 if (metadata)
544 user_id = metadata->GetUserID();
545
Sean Callanan00f43622011-11-18 03:28:09 +0000546 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
547 {
Sean Callanan48e894b2012-05-25 18:12:26 +0000548 std::string name_string;
549 llvm::raw_string_ostream name_stream(name_string);
550 from_named_decl->printName(name_stream);
551 name_stream.flush();
552
Daniel Malead01b2952012-11-29 21:49:15 +0000553 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000554 from->getDeclKindName(),
555 to,
Sean Callanan48e894b2012-05-25 18:12:26 +0000556 name_string.c_str(),
Sean Callanan60217122012-04-13 00:10:03 +0000557 from,
Jim Ingham379397632012-10-27 02:54:13 +0000558 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000559 }
560 else
561 {
Daniel Malead01b2952012-11-29 21:49:15 +0000562 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000563 from->getDeclKindName(),
564 to,
Sean Callanan60217122012-04-13 00:10:03 +0000565 from,
Jim Ingham379397632012-10-27 02:54:13 +0000566 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000567 }
568 }
569
Sean Callananb0b87a52011-11-16 22:23:28 +0000570 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
571 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000572
Sean Callananb0b87a52011-11-16 22:23:28 +0000573 if (from_context_md)
574 {
575 OriginMap &origins = from_context_md->m_origins;
576
577 OriginMap::iterator origin_iter = origins.find(from);
578
579 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000580 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000581 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000582
Sean Callanan5bc5a762011-12-20 23:55:47 +0000583 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
584
585 if (direct_completer.get() != this)
586 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
587
Sean Callanan00f43622011-11-18 03:28:09 +0000588 if (log)
589 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
590 origin_iter->second.decl,
591 origin_iter->second.ctx,
592 &from->getASTContext(),
593 &to->getASTContext());
594 }
595 else
596 {
Sean Callanan0eed0d42011-12-06 03:41:14 +0000597 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
598
Sean Callanan00f43622011-11-18 03:28:09 +0000599 if (log)
600 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
601 &from->getASTContext());
602 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000603
604 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
605 {
606 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
607
608 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
609
610 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
611
612 if (namespace_map_iter != namespace_maps.end())
613 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
614 }
615 }
616 else
617 {
618 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000619
620 if (log)
621 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
622 from,
623 m_source_ctx,
624 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000625 }
626
Sean Callanancc427fa2011-07-30 02:42:06 +0000627 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
628 {
629 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
630
631 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan3d654b32012-09-24 22:25:51 +0000632 to_tag_decl->setMustBuildLookupTable();
633
Sean Callanancc427fa2011-07-30 02:42:06 +0000634 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000635 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000636 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000637 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
638 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
639 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000640
641 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000642 }
643
Sean Callananb2269162011-10-21 22:18:07 +0000644 if (isa<NamespaceDecl>(from))
645 {
646 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
647
648 m_master.BuildNamespaceMap(to_namespace_decl);
649
650 to_namespace_decl->setHasExternalVisibleStorage();
651 }
652
Sean Callanan00f43622011-11-18 03:28:09 +0000653 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000654 {
655 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
Sean Callanana9bc0652012-01-19 02:17:40 +0000656
Sean Callanand5c17ed2011-11-15 02:11:17 +0000657 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000658 to_interface_decl->setHasExternalVisibleStorage();
Sean Callanan5b26f272012-02-04 08:49:35 +0000659
660 /*to_interface_decl->setExternallyCompleted();*/
Sean Callanan0eed0d42011-12-06 03:41:14 +0000661
Sean Callanand5c17ed2011-11-15 02:11:17 +0000662 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000663 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000664 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
665 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
Sean Callanan5b26f272012-02-04 08:49:35 +0000666 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000667 }
668
669 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000670}