blob: 57e5b216f620adbb0392ead3541d9cd3391c7a09 [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
Sean Callanan6b200d02013-03-21 22:15:41 +0000305bool
306ClangASTImporter::RequireCompleteType (clang::QualType type)
307{
308 if (type.isNull())
309 return false;
310
311 if (const TagType *tag_type = type->getAs<TagType>())
312 {
313 return CompleteTagDecl(tag_type->getDecl());
314 }
315 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>())
316 {
317 if (ObjCInterfaceDecl *objc_interface_decl = objc_object_type->getInterface())
318 return CompleteObjCInterfaceDecl(objc_interface_decl);
319 else
320 return false;
321 }
322 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
323 {
324 return RequireCompleteType(array_type->getElementType());
325 }
326 if (const AtomicType *atomic_type = type->getAs<AtomicType>())
327 {
328 return RequireCompleteType(atomic_type->getPointeeType());
329 }
330
331 return true;
332}
333
Jim Ingham379397632012-10-27 02:54:13 +0000334ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +0000335ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
336{
337 DeclOrigin decl_origin = GetDeclOrigin(decl);
338
339 if (decl_origin.Valid())
Greg Claytond0029442013-03-27 01:48:02 +0000340 return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
Sean Callanan60217122012-04-13 00:10:03 +0000341 else
Greg Claytond0029442013-03-27 01:48:02 +0000342 return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
Sean Callanan60217122012-04-13 00:10:03 +0000343}
344
Sean Callananf487bd82011-11-16 21:40:57 +0000345ClangASTImporter::DeclOrigin
346ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
347{
348 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
349
350 OriginMap &origins = context_md->m_origins;
351
352 OriginMap::iterator iter = origins.find(decl);
353
354 if (iter != origins.end())
355 return iter->second;
356 else
357 return DeclOrigin();
358}
359
Sean Callanan2cb5e522012-09-20 23:21:16 +0000360void
361ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
362{
363 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
364
365 OriginMap &origins = context_md->m_origins;
366
367 OriginMap::iterator iter = origins.find(decl);
368
369 if (iter != origins.end())
370 {
371 iter->second.decl = original_decl;
372 iter->second.ctx = &original_decl->getASTContext();
373 }
374 else
375 {
376 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
377 }
378}
379
380void
Sean Callanan503aa522011-10-12 00:12:34 +0000381ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
382 NamespaceMapSP &namespace_map)
383{
Sean Callananf487bd82011-11-16 21:40:57 +0000384 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
385
386 context_md->m_namespace_maps[decl] = namespace_map;
Sean Callanan503aa522011-10-12 00:12:34 +0000387}
388
389ClangASTImporter::NamespaceMapSP
390ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
391{
Sean Callananf487bd82011-11-16 21:40:57 +0000392 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
393
394 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
Sean Callanan503aa522011-10-12 00:12:34 +0000395
Sean Callananf487bd82011-11-16 21:40:57 +0000396 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
397
398 if (iter != namespace_maps.end())
Sean Callanan503aa522011-10-12 00:12:34 +0000399 return iter->second;
400 else
401 return NamespaceMapSP();
402}
403
Sean Callananb2269162011-10-21 22:18:07 +0000404void
405ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
406{
Jim Ingham28eb5712012-10-12 17:34:26 +0000407 assert (decl);
Sean Callananf487bd82011-11-16 21:40:57 +0000408 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
409
Sean Callananb2269162011-10-21 22:18:07 +0000410 const DeclContext *parent_context = decl->getDeclContext();
411 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
412 NamespaceMapSP parent_map;
413
414 if (parent_namespace)
415 parent_map = GetNamespaceMap(parent_namespace);
416
417 NamespaceMapSP new_map;
418
419 new_map.reset(new NamespaceMap);
420
Sean Callananf487bd82011-11-16 21:40:57 +0000421 if (context_md->m_map_completer)
Sean Callananb2269162011-10-21 22:18:07 +0000422 {
423 std::string namespace_string = decl->getDeclName().getAsString();
424
Sean Callananf487bd82011-11-16 21:40:57 +0000425 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
Sean Callananb2269162011-10-21 22:18:07 +0000426 }
427
Sean Callanan0eed0d42011-12-06 03:41:14 +0000428 context_md->m_namespace_maps[decl] = new_map;
Sean Callananb2269162011-10-21 22:18:07 +0000429}
430
Sean Callanan686b2312011-11-16 18:20:47 +0000431void
Sean Callanan99732312011-11-29 00:42:02 +0000432ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
Sean Callanan686b2312011-11-16 18:20:47 +0000433{
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000434 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
435
436 if (log)
437 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p", dst_ast);
438
Sean Callananf487bd82011-11-16 21:40:57 +0000439 m_metadata_map.erase(dst_ast);
Sean Callanan686b2312011-11-16 18:20:47 +0000440}
441
Sean Callanan99732312011-11-29 00:42:02 +0000442void
443ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
444{
445 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
446
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000447 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
448
449 if (log)
450 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p", src_ast, dst_ast);
451
Sean Callanan99732312011-11-29 00:42:02 +0000452 if (!md)
453 return;
454
455 md->m_minions.erase(src_ast);
456
457 for (OriginMap::iterator iter = md->m_origins.begin();
458 iter != md->m_origins.end();
459 )
460 {
461 if (iter->second.ctx == src_ast)
462 md->m_origins.erase(iter++);
463 else
464 ++iter;
465 }
466}
467
Sean Callanan0eed0d42011-12-06 03:41:14 +0000468ClangASTImporter::MapCompleter::~MapCompleter ()
Sean Callananb2269162011-10-21 22:18:07 +0000469{
470 return;
471}
472
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000473void
474ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
475{
476 ASTImporter::Imported(from, to);
Sean Callanan5b26f272012-02-04 08:49:35 +0000477
478 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
479
480 /*
481 if (to_objc_interface)
482 to_objc_interface->startDefinition();
483
484 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000485
Sean Callanan5b26f272012-02-04 08:49:35 +0000486 if (to_cxx_record)
487 to_cxx_record->startDefinition();
488 */
489
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000490 ImportDefinition(from);
Sean Callanan5b26f272012-02-04 08:49:35 +0000491
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000492 // If we're dealing with an Objective-C class, ensure that the inheritance has
493 // been set up correctly. The ASTImporter may not do this correctly if the
494 // class was originally sourced from symbols.
495
Sean Callanan5b26f272012-02-04 08:49:35 +0000496 if (to_objc_interface)
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000497 {
498 do
499 {
500 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
501
502 if (to_superclass)
503 break; // we're not going to override it if it's set
504
505 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
506
507 if (!from_objc_interface)
508 break;
509
510 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
511
512 if (!from_superclass)
513 break;
514
515 Decl *imported_from_superclass_decl = Import(from_superclass);
516
517 if (!imported_from_superclass_decl)
518 break;
519
520 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
521
522 if (!imported_from_superclass)
523 break;
524
Sean Callanan5b26f272012-02-04 08:49:35 +0000525 if (!to_objc_interface->hasDefinition())
526 to_objc_interface->startDefinition();
527
Sean Callanan2e93a2a2012-01-19 18:23:06 +0000528 to_objc_interface->setSuperClass(imported_from_superclass);
529 }
530 while (0);
531 }
Sean Callanancbbe3ac2012-01-13 22:55:55 +0000532}
533
Sean Callanancc427fa2011-07-30 02:42:06 +0000534clang::Decl
535*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
536{
Sean Callanan8106d802013-03-08 20:04:57 +0000537 ClangASTMetrics::RegisterClangImport();
538
Sean Callanancc427fa2011-07-30 02:42:06 +0000539 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan00f43622011-11-18 03:28:09 +0000540
541 if (log)
542 {
Jim Ingham379397632012-10-27 02:54:13 +0000543 lldb::user_id_t user_id;
544 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
545 if (metadata)
546 user_id = metadata->GetUserID();
547
Sean Callanan00f43622011-11-18 03:28:09 +0000548 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
549 {
Sean Callanan48e894b2012-05-25 18:12:26 +0000550 std::string name_string;
551 llvm::raw_string_ostream name_stream(name_string);
552 from_named_decl->printName(name_stream);
553 name_stream.flush();
554
Daniel Malead01b2952012-11-29 21:49:15 +0000555 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000556 from->getDeclKindName(),
557 to,
Sean Callanan48e894b2012-05-25 18:12:26 +0000558 name_string.c_str(),
Sean Callanan60217122012-04-13 00:10:03 +0000559 from,
Jim Ingham379397632012-10-27 02:54:13 +0000560 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000561 }
562 else
563 {
Daniel Malead01b2952012-11-29 21:49:15 +0000564 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000565 from->getDeclKindName(),
566 to,
Sean Callanan60217122012-04-13 00:10:03 +0000567 from,
Jim Ingham379397632012-10-27 02:54:13 +0000568 user_id);
Sean Callanan00f43622011-11-18 03:28:09 +0000569 }
570 }
571
Sean Callananb0b87a52011-11-16 22:23:28 +0000572 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
573 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
Sean Callananf487bd82011-11-16 21:40:57 +0000574
Sean Callananb0b87a52011-11-16 22:23:28 +0000575 if (from_context_md)
576 {
577 OriginMap &origins = from_context_md->m_origins;
578
579 OriginMap::iterator origin_iter = origins.find(from);
580
581 if (origin_iter != origins.end())
Sean Callanan00f43622011-11-18 03:28:09 +0000582 {
Sean Callananb0b87a52011-11-16 22:23:28 +0000583 to_context_md->m_origins[to] = origin_iter->second;
Sean Callanan00f43622011-11-18 03:28:09 +0000584
Sean Callanan5bc5a762011-12-20 23:55:47 +0000585 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
586
587 if (direct_completer.get() != this)
588 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
589
Sean Callanan00f43622011-11-18 03:28:09 +0000590 if (log)
591 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
592 origin_iter->second.decl,
593 origin_iter->second.ctx,
594 &from->getASTContext(),
595 &to->getASTContext());
596 }
597 else
598 {
Sean Callanan0eed0d42011-12-06 03:41:14 +0000599 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
600
Sean Callanan00f43622011-11-18 03:28:09 +0000601 if (log)
602 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
603 &from->getASTContext());
604 }
Sean Callananb0b87a52011-11-16 22:23:28 +0000605
606 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
607 {
608 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
609
610 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
611
612 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
613
614 if (namespace_map_iter != namespace_maps.end())
615 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
616 }
617 }
618 else
619 {
620 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
Sean Callanan00f43622011-11-18 03:28:09 +0000621
622 if (log)
623 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
624 from,
625 m_source_ctx,
626 &to->getASTContext());
Sean Callananb0b87a52011-11-16 22:23:28 +0000627 }
628
Sean Callanancc427fa2011-07-30 02:42:06 +0000629 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
630 {
631 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
632
633 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan3d654b32012-09-24 22:25:51 +0000634 to_tag_decl->setMustBuildLookupTable();
635
Sean Callanancc427fa2011-07-30 02:42:06 +0000636 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000637 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
Sean Callanancc427fa2011-07-30 02:42:06 +0000638 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000639 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
640 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
641 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanan00f43622011-11-18 03:28:09 +0000642
643 to_tag_decl = NULL;
Sean Callanancc427fa2011-07-30 02:42:06 +0000644 }
645
Sean Callananb2269162011-10-21 22:18:07 +0000646 if (isa<NamespaceDecl>(from))
647 {
648 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
649
650 m_master.BuildNamespaceMap(to_namespace_decl);
651
652 to_namespace_decl->setHasExternalVisibleStorage();
653 }
654
Sean Callanan00f43622011-11-18 03:28:09 +0000655 if (isa<ObjCInterfaceDecl>(from))
Sean Callanancc427fa2011-07-30 02:42:06 +0000656 {
657 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
Sean Callanana9bc0652012-01-19 02:17:40 +0000658
Sean Callanand5c17ed2011-11-15 02:11:17 +0000659 to_interface_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000660 to_interface_decl->setHasExternalVisibleStorage();
Sean Callanan5b26f272012-02-04 08:49:35 +0000661
662 /*to_interface_decl->setExternallyCompleted();*/
Sean Callanan0eed0d42011-12-06 03:41:14 +0000663
Sean Callanand5c17ed2011-11-15 02:11:17 +0000664 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000665 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000666 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
667 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
Sean Callanan5b26f272012-02-04 08:49:35 +0000668 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
Sean Callanancc427fa2011-07-30 02:42:06 +0000669 }
670
671 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000672}