]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Symbol/ClangASTImporter.cpp
Merge OpenSSL 1.0.2f.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Symbol / ClangASTImporter.cpp
1 //===-- 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"
11 #include "clang/AST/DeclCXX.h"
12 #include "clang/AST/DeclObjC.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Symbol/ClangASTContext.h"
17 #include "lldb/Symbol/ClangASTImporter.h"
18 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
19 #include "lldb/Symbol/ClangNamespaceDecl.h"
20 #include "lldb/Utility/LLDBAssert.h"
21
22 using namespace lldb_private;
23 using namespace clang;
24
25 ClangASTMetrics::Counters ClangASTMetrics::global_counters = { 0, 0, 0, 0, 0, 0 };
26 ClangASTMetrics::Counters ClangASTMetrics::local_counters = { 0, 0, 0, 0, 0, 0 };
27
28 void ClangASTMetrics::DumpCounters (Log *log, ClangASTMetrics::Counters &counters)
29 {
30     log->Printf("  Number of visible Decl queries by name     : %" PRIu64, counters.m_visible_query_count);
31     log->Printf("  Number of lexical Decl queries             : %" PRIu64, counters.m_lexical_query_count);
32     log->Printf("  Number of imports initiated by LLDB        : %" PRIu64, counters.m_lldb_import_count);
33     log->Printf("  Number of imports conducted by Clang       : %" PRIu64, counters.m_clang_import_count);
34     log->Printf("  Number of Decls completed                  : %" PRIu64, counters.m_decls_completed_count);
35     log->Printf("  Number of records laid out                 : %" PRIu64, counters.m_record_layout_count);
36 }
37
38 void ClangASTMetrics::DumpCounters (Log *log)
39 {
40     if (!log)
41         return;
42     
43     log->Printf("== ClangASTMetrics output ==");
44     log->Printf("-- Global metrics --");
45     DumpCounters (log, global_counters);
46     log->Printf("-- Local metrics --");
47     DumpCounters (log, local_counters);
48 }
49
50 clang::QualType
51 ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
52                             clang::ASTContext *src_ast,
53                             clang::QualType type)
54 {
55     MinionSP minion_sp (GetMinion(dst_ast, src_ast));
56     
57     if (minion_sp)
58         return minion_sp->Import(type);
59     
60     return QualType();
61 }
62
63 lldb::clang_type_t
64 ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
65                             clang::ASTContext *src_ast,
66                             lldb::clang_type_t type)
67 {
68     return CopyType (dst_ast, src_ast, QualType::getFromOpaquePtr(type)).getAsOpaquePtr();
69 }
70
71 clang::Decl *
72 ClangASTImporter::CopyDecl (clang::ASTContext *dst_ast,
73                             clang::ASTContext *src_ast,
74                             clang::Decl *decl)
75 {
76     MinionSP minion_sp;
77     
78     minion_sp = GetMinion(dst_ast, src_ast);
79     
80     if (minion_sp)
81     {
82         clang::Decl *result = minion_sp->Import(decl);
83         
84         if (!result)
85         {
86             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
87
88             if (log)
89             {
90                 lldb::user_id_t user_id = LLDB_INVALID_UID;
91                 ClangASTMetadata *metadata = GetDeclMetadata(decl);
92                 if (metadata)
93                     user_id = metadata->GetUserID();
94                 
95                 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
96                     log->Printf("  [ClangASTImporter] WARNING: Failed to import a %s '%s', metadata 0x%" PRIx64,
97                                 decl->getDeclKindName(),
98                                 named_decl->getNameAsString().c_str(),
99                                 user_id);
100                 else
101                     log->Printf("  [ClangASTImporter] WARNING: Failed to import a %s, metadata 0x%" PRIx64,
102                                 decl->getDeclKindName(),
103                                 user_id);
104             }
105         }
106         
107         return result;
108     }
109     
110     return nullptr;
111 }
112
113 class DeclContextOverride
114 {
115 private:
116     struct Backup
117     {
118         clang::DeclContext *decl_context;
119         clang::DeclContext *lexical_decl_context;
120     };
121     
122     std::map<clang::Decl *, Backup> m_backups;
123     
124     void OverrideOne(clang::Decl *decl)
125     {
126         if (m_backups.find(decl) != m_backups.end())
127         {
128             return;
129         }
130             
131         m_backups[decl] = { decl->getDeclContext(), decl->getLexicalDeclContext() };
132         
133         decl->setDeclContext(decl->getASTContext().getTranslationUnitDecl());
134         decl->setLexicalDeclContext(decl->getASTContext().getTranslationUnitDecl());
135     }
136     
137     bool ChainPassesThrough(clang::Decl *decl,
138                             clang::DeclContext *base,
139                             clang::DeclContext *(clang::Decl::*contextFromDecl)(),
140                             clang::DeclContext *(clang::DeclContext::*contextFromContext)())
141     {
142         for (DeclContext *decl_ctx = (decl->*contextFromDecl)();
143              decl_ctx;
144              decl_ctx = (decl_ctx->*contextFromContext)())
145         {
146             if (decl_ctx == base)
147             {
148                 return true;
149             }
150         }
151         
152         return false;
153     }
154     
155     clang::Decl *GetEscapedChild(clang::Decl *decl, clang::DeclContext *base = nullptr)
156     {
157         if (base)
158         {
159             // decl's DeclContext chains must pass through base.
160             
161             if (!ChainPassesThrough(decl, base, &clang::Decl::getDeclContext, &clang::DeclContext::getParent) ||
162                 !ChainPassesThrough(decl, base, &clang::Decl::getLexicalDeclContext, &clang::DeclContext::getLexicalParent))
163             {
164                 return decl;
165             }
166         }
167         else
168         {
169             base = clang::dyn_cast<clang::DeclContext>(decl);
170             
171             if (!base)
172             {
173                 return nullptr;
174             }
175         }
176         
177         if (clang::DeclContext *context = clang::dyn_cast<clang::DeclContext>(decl))
178         {
179             for (clang::Decl *decl : context->decls())
180             {
181                 if (clang::Decl *escaped_child = GetEscapedChild(decl))
182                 {
183                     return escaped_child;
184                 }
185             }
186         }
187         
188         return nullptr;
189     }
190     
191     void Override(clang::Decl *decl)
192     {
193         if (clang::Decl *escaped_child = GetEscapedChild(decl))
194         {
195             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
196             
197             if (log)
198                 log->Printf("    [ClangASTImporter] DeclContextOverride couldn't override (%sDecl*)%p - its child (%sDecl*)%p escapes",
199                             decl->getDeclKindName(), static_cast<void*>(decl),
200                             escaped_child->getDeclKindName(), static_cast<void*>(escaped_child));
201             lldbassert(0 && "Couldn't override!");
202         }
203         
204         OverrideOne(decl);
205     }
206     
207 public:
208     DeclContextOverride()
209     {
210     }
211     
212     void OverrideAllDeclsFromContainingFunction(clang::Decl *decl)
213     {
214         for (DeclContext *decl_context = decl->getLexicalDeclContext();
215              decl_context;
216              decl_context = decl_context->getLexicalParent())
217         {
218             DeclContext *redecl_context = decl_context->getRedeclContext();
219             
220             if (llvm::isa<FunctionDecl>(redecl_context) &&
221                 llvm::isa<TranslationUnitDecl>(redecl_context->getLexicalParent()))
222             {
223                 for (clang::Decl *child_decl : decl_context->decls())
224                 {
225                     Override(child_decl);
226                 }
227             }
228         }
229     }
230     
231     ~DeclContextOverride()
232     {
233         for (const std::pair<clang::Decl *, Backup> &backup : m_backups)
234         {
235             backup.first->setDeclContext(backup.second.decl_context);
236             backup.first->setLexicalDeclContext(backup.second.lexical_decl_context);
237         }
238     }
239 };
240
241 lldb::clang_type_t
242 ClangASTImporter::DeportType (clang::ASTContext *dst_ctx,
243                               clang::ASTContext *src_ctx,
244                               lldb::clang_type_t type)
245 {    
246     MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
247     
248     if (!minion_sp)
249         return nullptr;
250     
251     std::set<NamedDecl *> decls_to_deport;
252     std::set<NamedDecl *> decls_already_deported;
253     
254     DeclContextOverride decl_context_override;
255     
256     if (const clang::TagType *tag_type = clang::QualType::getFromOpaquePtr(type)->getAs<TagType>())
257     {
258         decl_context_override.OverrideAllDeclsFromContainingFunction(tag_type->getDecl());
259     }
260     
261     minion_sp->InitDeportWorkQueues(&decls_to_deport,
262                                     &decls_already_deported);
263     
264     lldb::clang_type_t result = CopyType(dst_ctx, src_ctx, type);
265     
266     minion_sp->ExecuteDeportWorkQueues();
267     
268     if (!result)
269         return nullptr;
270     
271     return result;
272
273 }
274
275 clang::Decl *
276 ClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx,
277                               clang::ASTContext *src_ctx,
278                               clang::Decl *decl)
279 {
280     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
281
282     if (log)
283         log->Printf("    [ClangASTImporter] DeportDecl called on (%sDecl*)%p from (ASTContext*)%p to (ASTContex*)%p",
284                     decl->getDeclKindName(), static_cast<void*>(decl),
285                     static_cast<void*>(src_ctx),
286                     static_cast<void*>(dst_ctx));
287
288     MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
289
290     if (!minion_sp)
291         return nullptr;
292
293     std::set<NamedDecl *> decls_to_deport;
294     std::set<NamedDecl *> decls_already_deported;
295     
296     DeclContextOverride decl_context_override;
297     
298     decl_context_override.OverrideAllDeclsFromContainingFunction(decl);
299
300     minion_sp->InitDeportWorkQueues(&decls_to_deport,
301                                     &decls_already_deported);
302
303     clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
304
305     minion_sp->ExecuteDeportWorkQueues();
306
307     if (!result)
308         return nullptr;
309
310     if (log)
311         log->Printf("    [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p",
312                     decl->getDeclKindName(), static_cast<void*>(decl),
313                     result->getDeclKindName(), static_cast<void*>(result));
314
315     return result;
316 }
317
318 void
319 ClangASTImporter::CompleteDecl (clang::Decl *decl)
320 {
321     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
322
323     if (log)
324         log->Printf("    [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
325                     decl->getDeclKindName(), static_cast<void*>(decl));
326
327     if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl)) 
328     {
329         if (!interface_decl->getDefinition())
330         {
331             interface_decl->startDefinition();
332             CompleteObjCInterfaceDecl(interface_decl);
333         }
334     }
335     else if (ObjCProtocolDecl *protocol_decl = dyn_cast<ObjCProtocolDecl>(decl)) 
336     {
337         if (!protocol_decl->getDefinition())
338             protocol_decl->startDefinition();
339     }
340     else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) 
341     {
342         if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined()) 
343         {
344             tag_decl->startDefinition();
345             CompleteTagDecl(tag_decl);
346             tag_decl->setCompleteDefinition(true);
347         }
348     }
349     else
350     {
351         assert (0 && "CompleteDecl called on a Decl that can't be completed");
352     }
353 }
354
355 bool
356 ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
357 {
358     ClangASTMetrics::RegisterDeclCompletion();
359     
360     DeclOrigin decl_origin = GetDeclOrigin(decl);
361     
362     if (!decl_origin.Valid())
363         return false;
364     
365     if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
366         return false;
367     
368     MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
369     
370     if (minion_sp)
371         minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
372         
373     return true;
374 }
375
376 bool
377 ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
378 {
379     ClangASTMetrics::RegisterDeclCompletion();
380
381     clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
382         
383     if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
384         return false;
385     
386     MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
387     
388     if (minion_sp)
389         minion_sp->ImportDefinitionTo(decl, origin_decl);
390         
391     ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
392
393     OriginMap &origins = context_md->m_origins;
394
395     origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
396     
397     return true;
398 }
399
400 bool
401 ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
402 {
403     ClangASTMetrics::RegisterDeclCompletion();
404     
405     DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
406     
407     if (!decl_origin.Valid())
408         return false;
409     
410     if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
411         return false;
412     
413     MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
414     
415     if (minion_sp)
416         minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
417
418     if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
419         RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
420
421     return true;
422 }
423
424 bool
425 ClangASTImporter::RequireCompleteType (clang::QualType type)
426 {
427     if (type.isNull())
428         return false;
429     
430     if (const TagType *tag_type = type->getAs<TagType>())
431     {
432         TagDecl *tag_decl = tag_type->getDecl();
433
434         if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
435             return true;
436
437         return CompleteTagDecl(tag_decl);
438     }
439     if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>())
440     {
441         if (ObjCInterfaceDecl *objc_interface_decl = objc_object_type->getInterface())
442             return CompleteObjCInterfaceDecl(objc_interface_decl);
443         else
444             return false;
445     }
446     if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
447     {
448         return RequireCompleteType(array_type->getElementType());
449     }
450     if (const AtomicType *atomic_type = type->getAs<AtomicType>())
451     {
452         return RequireCompleteType(atomic_type->getPointeeType());
453     }
454     
455     return true;
456 }
457
458 ClangASTMetadata *
459 ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
460 {
461     DeclOrigin decl_origin = GetDeclOrigin(decl);
462     
463     if (decl_origin.Valid())
464         return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
465     else
466         return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
467 }
468
469 ClangASTImporter::DeclOrigin
470 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
471 {
472     ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
473     
474     OriginMap &origins = context_md->m_origins;
475     
476     OriginMap::iterator iter = origins.find(decl);
477     
478     if (iter != origins.end())
479         return iter->second;
480     else
481         return DeclOrigin();
482 }
483
484 void
485 ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
486 {
487     ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
488     
489     OriginMap &origins = context_md->m_origins;
490     
491     OriginMap::iterator iter = origins.find(decl);
492     
493     if (iter != origins.end())
494     {
495         iter->second.decl = original_decl;
496         iter->second.ctx = &original_decl->getASTContext();
497     }
498     else
499     {
500         origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
501     }
502 }
503
504 void
505 ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl, 
506                                        NamespaceMapSP &namespace_map)
507 {
508     ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
509     
510     context_md->m_namespace_maps[decl] = namespace_map;
511 }
512
513 ClangASTImporter::NamespaceMapSP 
514 ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
515 {
516     ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
517
518     NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
519     
520     NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
521     
522     if (iter != namespace_maps.end())
523         return iter->second;
524     else
525         return NamespaceMapSP();
526 }
527
528 void 
529 ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
530 {
531     assert (decl);
532     ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
533
534     const DeclContext *parent_context = decl->getDeclContext();
535     const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
536     NamespaceMapSP parent_map;
537     
538     if (parent_namespace)
539         parent_map = GetNamespaceMap(parent_namespace);
540     
541     NamespaceMapSP new_map;
542     
543     new_map.reset(new NamespaceMap);
544  
545     if (context_md->m_map_completer)
546     {
547         std::string namespace_string = decl->getDeclName().getAsString();
548     
549         context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
550     }
551     
552     context_md->m_namespace_maps[decl] = new_map;
553 }
554
555 void 
556 ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
557 {
558     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
559
560     if (log)
561         log->Printf("    [ClangASTImporter] Forgetting destination (ASTContext*)%p",
562                     static_cast<void*>(dst_ast));
563
564     m_metadata_map.erase(dst_ast);
565 }
566
567 void
568 ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
569 {
570     ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
571
572     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
573
574     if (log)
575         log->Printf("    [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p",
576                     static_cast<void*>(src_ast), static_cast<void*>(dst_ast));
577
578     if (!md)
579         return;
580
581     md->m_minions.erase(src_ast);
582
583     for (OriginMap::iterator iter = md->m_origins.begin();
584          iter != md->m_origins.end();
585          )
586     {
587         if (iter->second.ctx == src_ast)
588             md->m_origins.erase(iter++);
589         else
590             ++iter;
591     }
592 }
593
594 ClangASTImporter::MapCompleter::~MapCompleter ()
595 {
596     return;
597 }
598
599 void
600 ClangASTImporter::Minion::InitDeportWorkQueues (std::set<clang::NamedDecl *> *decls_to_deport,
601                                                 std::set<clang::NamedDecl *> *decls_already_deported)
602 {
603     assert(!m_decls_to_deport); // TODO make debug only
604     assert(!m_decls_already_deported);
605     
606     m_decls_to_deport = decls_to_deport;
607     m_decls_already_deported = decls_already_deported;
608 }
609
610 void
611 ClangASTImporter::Minion::ExecuteDeportWorkQueues ()
612 {
613     assert(m_decls_to_deport); // TODO make debug only
614     assert(m_decls_already_deported);
615     
616     ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&getToContext());
617         
618     while (!m_decls_to_deport->empty())
619     {
620         NamedDecl *decl = *m_decls_to_deport->begin();
621         
622         m_decls_already_deported->insert(decl);
623         m_decls_to_deport->erase(decl);
624         
625         DeclOrigin &origin = to_context_md->m_origins[decl];
626         
627         assert (origin.ctx == m_source_ctx);    // otherwise we should never have added this
628                                                 // because it doesn't need to be deported
629         
630         Decl *original_decl = to_context_md->m_origins[decl].decl;
631         
632         ClangASTContext::GetCompleteDecl (m_source_ctx, original_decl);
633         
634         if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
635         {
636             if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
637                 if (original_tag_decl->isCompleteDefinition())
638                     ImportDefinitionTo(tag_decl, original_tag_decl);
639             
640             tag_decl->setHasExternalLexicalStorage(false);
641             tag_decl->setHasExternalVisibleStorage(false);
642         }
643         else if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
644         {
645             interface_decl->setHasExternalLexicalStorage(false);
646             interface_decl->setHasExternalVisibleStorage(false);
647         }
648         
649         to_context_md->m_origins.erase(decl);
650     }
651     
652     m_decls_to_deport = nullptr;
653     m_decls_already_deported = nullptr;
654 }
655
656 void
657 ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
658 {
659     ASTImporter::Imported(from, to);
660
661     ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
662
663     /*
664     if (to_objc_interface)
665         to_objc_interface->startDefinition();
666  
667     CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
668     
669     if (to_cxx_record)
670         to_cxx_record->startDefinition();
671     */
672
673     ImportDefinition(from);
674      
675     // If we're dealing with an Objective-C class, ensure that the inheritance has
676     // been set up correctly.  The ASTImporter may not do this correctly if the 
677     // class was originally sourced from symbols.
678     
679     if (to_objc_interface)
680     {
681         do
682         {
683             ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
684
685             if (to_superclass)
686                 break; // we're not going to override it if it's set
687             
688             ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
689             
690             if (!from_objc_interface)
691                 break;
692             
693             ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
694             
695             if (!from_superclass)
696                 break;
697             
698             Decl *imported_from_superclass_decl = Import(from_superclass);
699                 
700             if (!imported_from_superclass_decl)
701                 break;
702                 
703             ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
704             
705             if (!imported_from_superclass)
706                 break;
707             
708             if (!to_objc_interface->hasDefinition())
709                 to_objc_interface->startDefinition();
710             
711             to_objc_interface->setSuperClass(
712                     m_source_ctx->getTrivialTypeSourceInfo(m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
713         }
714         while (0);
715     }
716 }
717
718 clang::Decl *
719 ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
720 {
721     ClangASTMetrics::RegisterClangImport();
722
723     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
724
725     lldb::user_id_t user_id = LLDB_INVALID_UID;
726     ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
727     if (metadata)
728         user_id = metadata->GetUserID();
729     
730     if (log)
731     {
732         if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
733         {
734             std::string name_string;
735             llvm::raw_string_ostream name_stream(name_string);
736             from_named_decl->printName(name_stream);
737             name_stream.flush();
738
739             log->Printf("    [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64,
740                         from->getDeclKindName(), static_cast<void*>(to),
741                         name_string.c_str(), static_cast<void*>(from),
742                         user_id);
743         }
744         else
745         {
746             log->Printf("    [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64,
747                         from->getDeclKindName(), static_cast<void*>(to),
748                         static_cast<void*>(from), user_id);
749         }
750     }
751
752     ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
753     ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
754
755     if (from_context_md)
756     {
757         OriginMap &origins = from_context_md->m_origins;
758
759         OriginMap::iterator origin_iter = origins.find(from);
760
761         if (origin_iter != origins.end())
762         {
763             if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
764                 user_id != LLDB_INVALID_UID)
765             {
766                 to_context_md->m_origins[to] = origin_iter->second;
767             }
768                 
769             MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
770
771             if (direct_completer.get() != this)
772                 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
773
774             if (log)
775                 log->Printf("    [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
776                             static_cast<void*>(origin_iter->second.decl),
777                             static_cast<void*>(origin_iter->second.ctx),
778                             static_cast<void*>(&from->getASTContext()),
779                             static_cast<void*>(&to->getASTContext()));
780         }
781         else
782         {
783             if (m_decls_to_deport && m_decls_already_deported)
784             {
785                 if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to))
786                 {
787                     NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
788
789                     if (!m_decls_already_deported->count(to_named_decl))
790                         m_decls_to_deport->insert(to_named_decl);
791                 }
792             }
793             
794             if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
795                 user_id != LLDB_INVALID_UID)
796             {
797                 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
798             }
799
800             if (log)
801                 log->Printf("    [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
802                             static_cast<void*>(&from->getASTContext()));
803         }
804
805         if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
806         {
807             clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
808
809             NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
810
811             NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
812
813             if (namespace_map_iter != namespace_maps.end())
814                 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
815         }
816     }
817     else
818     {
819         to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
820
821         if (log)
822             log->Printf("    [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
823                         static_cast<void*>(from),
824                         static_cast<void*>(m_source_ctx),
825                         static_cast<void*>(&to->getASTContext()));
826     }
827
828     if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
829     {
830         TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
831
832         to_tag_decl->setHasExternalLexicalStorage();
833         to_tag_decl->setMustBuildLookupTable();
834
835         if (log)
836             log->Printf("    [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
837                         (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
838                         (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
839                         (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
840                         (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
841     }
842
843     if (isa<NamespaceDecl>(from))
844     {
845         NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
846
847         m_master.BuildNamespaceMap(to_namespace_decl);
848
849         to_namespace_decl->setHasExternalVisibleStorage();
850     }
851
852     if (isa<ObjCInterfaceDecl>(from))
853     {
854         ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
855
856         to_interface_decl->setHasExternalLexicalStorage();
857         to_interface_decl->setHasExternalVisibleStorage();
858
859         /*to_interface_decl->setExternallyCompleted();*/
860
861         if (log)
862             log->Printf("    [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
863                         (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
864                         (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
865                         (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
866     }
867
868     return clang::ASTImporter::Imported(from, to);
869 }
870
871 clang::Decl *ClangASTImporter::Minion::GetOriginalDecl (clang::Decl *To)
872 {
873     ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&To->getASTContext());
874     
875     if (!to_context_md)
876         return nullptr;
877     
878     OriginMap::iterator iter = to_context_md->m_origins.find(To);
879     
880     if (iter == to_context_md->m_origins.end())
881         return nullptr;
882     
883     return const_cast<clang::Decl*>(iter->second.decl);
884 }