]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Symbol/ClangASTImporter.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / 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 "lldb/Symbol/ClangASTImporter.h"
11 #include "lldb/Core/Log.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Symbol/ClangASTContext.h"
14 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
15 #include "lldb/Symbol/ClangUtil.h"
16 #include "lldb/Utility/LLDBAssert.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "llvm/Support/raw_ostream.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,
29                                    ClangASTMetrics::Counters &counters) {
30   log->Printf("  Number of visible Decl queries by name     : %" PRIu64,
31               counters.m_visible_query_count);
32   log->Printf("  Number of lexical Decl queries             : %" PRIu64,
33               counters.m_lexical_query_count);
34   log->Printf("  Number of imports initiated by LLDB        : %" PRIu64,
35               counters.m_lldb_import_count);
36   log->Printf("  Number of imports conducted by Clang       : %" PRIu64,
37               counters.m_clang_import_count);
38   log->Printf("  Number of Decls completed                  : %" PRIu64,
39               counters.m_decls_completed_count);
40   log->Printf("  Number of records laid out                 : %" PRIu64,
41               counters.m_record_layout_count);
42 }
43
44 void ClangASTMetrics::DumpCounters(Log *log) {
45   if (!log)
46     return;
47
48   log->Printf("== ClangASTMetrics output ==");
49   log->Printf("-- Global metrics --");
50   DumpCounters(log, global_counters);
51   log->Printf("-- Local metrics --");
52   DumpCounters(log, local_counters);
53 }
54
55 clang::QualType ClangASTImporter::CopyType(clang::ASTContext *dst_ast,
56                                            clang::ASTContext *src_ast,
57                                            clang::QualType type) {
58   MinionSP minion_sp(GetMinion(dst_ast, src_ast));
59
60   if (minion_sp)
61     return minion_sp->Import(type);
62
63   return QualType();
64 }
65
66 lldb::opaque_compiler_type_t
67 ClangASTImporter::CopyType(clang::ASTContext *dst_ast,
68                            clang::ASTContext *src_ast,
69                            lldb::opaque_compiler_type_t type) {
70   return CopyType(dst_ast, src_ast, QualType::getFromOpaquePtr(type))
71       .getAsOpaquePtr();
72 }
73
74 CompilerType ClangASTImporter::CopyType(ClangASTContext &dst_ast,
75                                         const CompilerType &src_type) {
76   clang::ASTContext *dst_clang_ast = dst_ast.getASTContext();
77   if (dst_clang_ast) {
78     ClangASTContext *src_ast =
79         llvm::dyn_cast_or_null<ClangASTContext>(src_type.GetTypeSystem());
80     if (src_ast) {
81       clang::ASTContext *src_clang_ast = src_ast->getASTContext();
82       if (src_clang_ast) {
83         lldb::opaque_compiler_type_t dst_clang_type = CopyType(
84             dst_clang_ast, src_clang_ast, src_type.GetOpaqueQualType());
85
86         if (dst_clang_type)
87           return CompilerType(&dst_ast, dst_clang_type);
88       }
89     }
90   }
91   return CompilerType();
92 }
93
94 clang::Decl *ClangASTImporter::CopyDecl(clang::ASTContext *dst_ast,
95                                         clang::ASTContext *src_ast,
96                                         clang::Decl *decl) {
97   MinionSP minion_sp;
98
99   minion_sp = GetMinion(dst_ast, src_ast);
100
101   if (minion_sp) {
102     clang::Decl *result = minion_sp->Import(decl);
103
104     if (!result) {
105       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
106
107       if (log) {
108         lldb::user_id_t user_id = LLDB_INVALID_UID;
109         ClangASTMetadata *metadata = GetDeclMetadata(decl);
110         if (metadata)
111           user_id = metadata->GetUserID();
112
113         if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
114           log->Printf("  [ClangASTImporter] WARNING: Failed to import a %s "
115                       "'%s', metadata 0x%" PRIx64,
116                       decl->getDeclKindName(),
117                       named_decl->getNameAsString().c_str(), user_id);
118         else
119           log->Printf("  [ClangASTImporter] WARNING: Failed to import a %s, "
120                       "metadata 0x%" PRIx64,
121                       decl->getDeclKindName(), user_id);
122       }
123     }
124
125     return result;
126   }
127
128   return nullptr;
129 }
130
131 class DeclContextOverride {
132 private:
133   struct Backup {
134     clang::DeclContext *decl_context;
135     clang::DeclContext *lexical_decl_context;
136   };
137
138   std::map<clang::Decl *, Backup> m_backups;
139
140   void OverrideOne(clang::Decl *decl) {
141     if (m_backups.find(decl) != m_backups.end()) {
142       return;
143     }
144
145     m_backups[decl] = {decl->getDeclContext(), decl->getLexicalDeclContext()};
146
147     decl->setDeclContext(decl->getASTContext().getTranslationUnitDecl());
148     decl->setLexicalDeclContext(decl->getASTContext().getTranslationUnitDecl());
149   }
150
151   bool ChainPassesThrough(
152       clang::Decl *decl, clang::DeclContext *base,
153       clang::DeclContext *(clang::Decl::*contextFromDecl)(),
154       clang::DeclContext *(clang::DeclContext::*contextFromContext)()) {
155     for (DeclContext *decl_ctx = (decl->*contextFromDecl)(); decl_ctx;
156          decl_ctx = (decl_ctx->*contextFromContext)()) {
157       if (decl_ctx == base) {
158         return true;
159       }
160     }
161
162     return false;
163   }
164
165   clang::Decl *GetEscapedChild(clang::Decl *decl,
166                                clang::DeclContext *base = nullptr) {
167     if (base) {
168       // decl's DeclContext chains must pass through base.
169
170       if (!ChainPassesThrough(decl, base, &clang::Decl::getDeclContext,
171                               &clang::DeclContext::getParent) ||
172           !ChainPassesThrough(decl, base, &clang::Decl::getLexicalDeclContext,
173                               &clang::DeclContext::getLexicalParent)) {
174         return decl;
175       }
176     } else {
177       base = clang::dyn_cast<clang::DeclContext>(decl);
178
179       if (!base) {
180         return nullptr;
181       }
182     }
183
184     if (clang::DeclContext *context =
185             clang::dyn_cast<clang::DeclContext>(decl)) {
186       for (clang::Decl *decl : context->decls()) {
187         if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
188           return escaped_child;
189         }
190       }
191     }
192
193     return nullptr;
194   }
195
196   void Override(clang::Decl *decl) {
197     if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
198       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
199
200       if (log)
201         log->Printf("    [ClangASTImporter] DeclContextOverride couldn't "
202                     "override (%sDecl*)%p - its child (%sDecl*)%p escapes",
203                     decl->getDeclKindName(), static_cast<void *>(decl),
204                     escaped_child->getDeclKindName(),
205                     static_cast<void *>(escaped_child));
206       lldbassert(0 && "Couldn't override!");
207     }
208
209     OverrideOne(decl);
210   }
211
212 public:
213   DeclContextOverride() {}
214
215   void OverrideAllDeclsFromContainingFunction(clang::Decl *decl) {
216     for (DeclContext *decl_context = decl->getLexicalDeclContext();
217          decl_context; decl_context = decl_context->getLexicalParent()) {
218       DeclContext *redecl_context = decl_context->getRedeclContext();
219
220       if (llvm::isa<FunctionDecl>(redecl_context) &&
221           llvm::isa<TranslationUnitDecl>(redecl_context->getLexicalParent())) {
222         for (clang::Decl *child_decl : decl_context->decls()) {
223           Override(child_decl);
224         }
225       }
226     }
227   }
228
229   ~DeclContextOverride() {
230     for (const std::pair<clang::Decl *, Backup> &backup : m_backups) {
231       backup.first->setDeclContext(backup.second.decl_context);
232       backup.first->setLexicalDeclContext(backup.second.lexical_decl_context);
233     }
234   }
235 };
236
237 lldb::opaque_compiler_type_t
238 ClangASTImporter::DeportType(clang::ASTContext *dst_ctx,
239                              clang::ASTContext *src_ctx,
240                              lldb::opaque_compiler_type_t type) {
241   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
242
243   if (log)
244     log->Printf("    [ClangASTImporter] DeportType called on (%sType*)0x%llx "
245                 "from (ASTContext*)%p to (ASTContext*)%p",
246                 QualType::getFromOpaquePtr(type)->getTypeClassName(),
247                 (unsigned long long)type, static_cast<void *>(src_ctx),
248                 static_cast<void *>(dst_ctx));
249
250   MinionSP minion_sp(GetMinion(dst_ctx, src_ctx));
251
252   if (!minion_sp)
253     return nullptr;
254
255   std::set<NamedDecl *> decls_to_deport;
256   std::set<NamedDecl *> decls_already_deported;
257
258   DeclContextOverride decl_context_override;
259
260   if (const clang::TagType *tag_type =
261           clang::QualType::getFromOpaquePtr(type)->getAs<TagType>()) {
262     decl_context_override.OverrideAllDeclsFromContainingFunction(
263         tag_type->getDecl());
264   }
265
266   minion_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);
267
268   lldb::opaque_compiler_type_t result = CopyType(dst_ctx, src_ctx, type);
269
270   minion_sp->ExecuteDeportWorkQueues();
271
272   if (!result)
273     return nullptr;
274
275   return result;
276 }
277
278 clang::Decl *ClangASTImporter::DeportDecl(clang::ASTContext *dst_ctx,
279                                           clang::ASTContext *src_ctx,
280                                           clang::Decl *decl) {
281   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
282
283   if (log)
284     log->Printf("    [ClangASTImporter] DeportDecl called on (%sDecl*)%p from "
285                 "(ASTContext*)%p to (ASTContext*)%p",
286                 decl->getDeclKindName(), static_cast<void *>(decl),
287                 static_cast<void *>(src_ctx), static_cast<void *>(dst_ctx));
288
289   MinionSP minion_sp(GetMinion(dst_ctx, src_ctx));
290
291   if (!minion_sp)
292     return nullptr;
293
294   std::set<NamedDecl *> decls_to_deport;
295   std::set<NamedDecl *> decls_already_deported;
296
297   DeclContextOverride decl_context_override;
298
299   decl_context_override.OverrideAllDeclsFromContainingFunction(decl);
300
301   minion_sp->InitDeportWorkQueues(&decls_to_deport, &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(
312         "    [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p",
313         decl->getDeclKindName(), static_cast<void *>(decl),
314         result->getDeclKindName(), static_cast<void *>(result));
315
316   return result;
317 }
318
319 bool ClangASTImporter::CanImport(const CompilerType &type) {
320   if (!ClangUtil::IsClangType(type))
321     return false;
322
323   // TODO: remove external completion BOOL
324   // CompleteAndFetchChildren should get the Decl out and check for the
325
326   clang::QualType qual_type(
327       ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
328
329   const clang::Type::TypeClass type_class = qual_type->getTypeClass();
330   switch (type_class) {
331   case clang::Type::Record: {
332     const clang::CXXRecordDecl *cxx_record_decl =
333         qual_type->getAsCXXRecordDecl();
334     if (cxx_record_decl) {
335       if (ResolveDeclOrigin(cxx_record_decl, NULL, NULL))
336         return true;
337     }
338   } break;
339
340   case clang::Type::Enum: {
341     clang::EnumDecl *enum_decl =
342         llvm::cast<clang::EnumType>(qual_type)->getDecl();
343     if (enum_decl) {
344       if (ResolveDeclOrigin(enum_decl, NULL, NULL))
345         return true;
346     }
347   } break;
348
349   case clang::Type::ObjCObject:
350   case clang::Type::ObjCInterface: {
351     const clang::ObjCObjectType *objc_class_type =
352         llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
353     if (objc_class_type) {
354       clang::ObjCInterfaceDecl *class_interface_decl =
355           objc_class_type->getInterface();
356       // We currently can't complete objective C types through the newly added
357       // ASTContext
358       // because it only supports TagDecl objects right now...
359       if (class_interface_decl) {
360         if (ResolveDeclOrigin(class_interface_decl, NULL, NULL))
361           return true;
362       }
363     }
364   } break;
365
366   case clang::Type::Typedef:
367     return CanImport(CompilerType(type.GetTypeSystem(),
368                                   llvm::cast<clang::TypedefType>(qual_type)
369                                       ->getDecl()
370                                       ->getUnderlyingType()
371                                       .getAsOpaquePtr()));
372
373   case clang::Type::Auto:
374     return CanImport(CompilerType(type.GetTypeSystem(),
375                                   llvm::cast<clang::AutoType>(qual_type)
376                                       ->getDeducedType()
377                                       .getAsOpaquePtr()));
378
379   case clang::Type::Elaborated:
380     return CanImport(CompilerType(type.GetTypeSystem(),
381                                   llvm::cast<clang::ElaboratedType>(qual_type)
382                                       ->getNamedType()
383                                       .getAsOpaquePtr()));
384
385   case clang::Type::Paren:
386     return CanImport(CompilerType(
387         type.GetTypeSystem(),
388         llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
389
390   default:
391     break;
392   }
393
394   return false;
395 }
396
397 bool ClangASTImporter::Import(const CompilerType &type) {
398   if (!ClangUtil::IsClangType(type))
399     return false;
400   // TODO: remove external completion BOOL
401   // CompleteAndFetchChildren should get the Decl out and check for the
402
403   clang::QualType qual_type(
404       ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
405
406   const clang::Type::TypeClass type_class = qual_type->getTypeClass();
407   switch (type_class) {
408   case clang::Type::Record: {
409     const clang::CXXRecordDecl *cxx_record_decl =
410         qual_type->getAsCXXRecordDecl();
411     if (cxx_record_decl) {
412       if (ResolveDeclOrigin(cxx_record_decl, NULL, NULL))
413         return CompleteAndFetchChildren(qual_type);
414     }
415   } break;
416
417   case clang::Type::Enum: {
418     clang::EnumDecl *enum_decl =
419         llvm::cast<clang::EnumType>(qual_type)->getDecl();
420     if (enum_decl) {
421       if (ResolveDeclOrigin(enum_decl, NULL, NULL))
422         return CompleteAndFetchChildren(qual_type);
423     }
424   } break;
425
426   case clang::Type::ObjCObject:
427   case clang::Type::ObjCInterface: {
428     const clang::ObjCObjectType *objc_class_type =
429         llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
430     if (objc_class_type) {
431       clang::ObjCInterfaceDecl *class_interface_decl =
432           objc_class_type->getInterface();
433       // We currently can't complete objective C types through the newly added
434       // ASTContext
435       // because it only supports TagDecl objects right now...
436       if (class_interface_decl) {
437         if (ResolveDeclOrigin(class_interface_decl, NULL, NULL))
438           return CompleteAndFetchChildren(qual_type);
439       }
440     }
441   } break;
442
443   case clang::Type::Typedef:
444     return Import(CompilerType(type.GetTypeSystem(),
445                                llvm::cast<clang::TypedefType>(qual_type)
446                                    ->getDecl()
447                                    ->getUnderlyingType()
448                                    .getAsOpaquePtr()));
449
450   case clang::Type::Auto:
451     return Import(CompilerType(type.GetTypeSystem(),
452                                llvm::cast<clang::AutoType>(qual_type)
453                                    ->getDeducedType()
454                                    .getAsOpaquePtr()));
455
456   case clang::Type::Elaborated:
457     return Import(CompilerType(type.GetTypeSystem(),
458                                llvm::cast<clang::ElaboratedType>(qual_type)
459                                    ->getNamedType()
460                                    .getAsOpaquePtr()));
461
462   case clang::Type::Paren:
463     return Import(CompilerType(
464         type.GetTypeSystem(),
465         llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
466
467   default:
468     break;
469   }
470   return false;
471 }
472
473 bool ClangASTImporter::CompleteType(const CompilerType &compiler_type) {
474   if (!CanImport(compiler_type))
475     return false;
476
477   if (Import(compiler_type)) {
478     ClangASTContext::CompleteTagDeclarationDefinition(compiler_type);
479     return true;
480   }
481
482   ClangASTContext::SetHasExternalStorage(compiler_type.GetOpaqueQualType(),
483                                          false);
484   return false;
485 }
486
487 bool ClangASTImporter::LayoutRecordType(
488     const clang::RecordDecl *record_decl, uint64_t &bit_size,
489     uint64_t &alignment,
490     llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
491     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
492         &base_offsets,
493     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
494         &vbase_offsets) {
495   RecordDeclToLayoutMap::iterator pos =
496       m_record_decl_to_layout_map.find(record_decl);
497   bool success = false;
498   base_offsets.clear();
499   vbase_offsets.clear();
500   if (pos != m_record_decl_to_layout_map.end()) {
501     bit_size = pos->second.bit_size;
502     alignment = pos->second.alignment;
503     field_offsets.swap(pos->second.field_offsets);
504     base_offsets.swap(pos->second.base_offsets);
505     vbase_offsets.swap(pos->second.vbase_offsets);
506     m_record_decl_to_layout_map.erase(pos);
507     success = true;
508   } else {
509     bit_size = 0;
510     alignment = 0;
511     field_offsets.clear();
512   }
513   return success;
514 }
515
516 void ClangASTImporter::InsertRecordDecl(clang::RecordDecl *decl,
517                                         const LayoutInfo &layout) {
518   m_record_decl_to_layout_map.insert(std::make_pair(decl, layout));
519 }
520
521 void ClangASTImporter::CompleteDecl(clang::Decl *decl) {
522   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
523
524   if (log)
525     log->Printf("    [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
526                 decl->getDeclKindName(), static_cast<void *>(decl));
527
528   if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl)) {
529     if (!interface_decl->getDefinition()) {
530       interface_decl->startDefinition();
531       CompleteObjCInterfaceDecl(interface_decl);
532     }
533   } else if (ObjCProtocolDecl *protocol_decl =
534                  dyn_cast<ObjCProtocolDecl>(decl)) {
535     if (!protocol_decl->getDefinition())
536       protocol_decl->startDefinition();
537   } else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) {
538     if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined()) {
539       tag_decl->startDefinition();
540       CompleteTagDecl(tag_decl);
541       tag_decl->setCompleteDefinition(true);
542     }
543   } else {
544     assert(0 && "CompleteDecl called on a Decl that can't be completed");
545   }
546 }
547
548 bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
549   ClangASTMetrics::RegisterDeclCompletion();
550
551   DeclOrigin decl_origin = GetDeclOrigin(decl);
552
553   if (!decl_origin.Valid())
554     return false;
555
556   if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
557     return false;
558
559   MinionSP minion_sp(GetMinion(&decl->getASTContext(), decl_origin.ctx));
560
561   if (minion_sp)
562     minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
563
564   return true;
565 }
566
567 bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl,
568                                                  clang::TagDecl *origin_decl) {
569   ClangASTMetrics::RegisterDeclCompletion();
570
571   clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
572
573   if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
574     return false;
575
576   MinionSP minion_sp(GetMinion(&decl->getASTContext(), origin_ast_ctx));
577
578   if (minion_sp)
579     minion_sp->ImportDefinitionTo(decl, origin_decl);
580
581   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
582
583   OriginMap &origins = context_md->m_origins;
584
585   origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
586
587   return true;
588 }
589
590 bool ClangASTImporter::CompleteObjCInterfaceDecl(
591     clang::ObjCInterfaceDecl *interface_decl) {
592   ClangASTMetrics::RegisterDeclCompletion();
593
594   DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
595
596   if (!decl_origin.Valid())
597     return false;
598
599   if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
600     return false;
601
602   MinionSP minion_sp(
603       GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
604
605   if (minion_sp)
606     minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
607
608   if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
609     RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
610
611   return true;
612 }
613
614 bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
615   if (!RequireCompleteType(type))
616     return false;
617
618   if (const TagType *tag_type = type->getAs<TagType>()) {
619     TagDecl *tag_decl = tag_type->getDecl();
620
621     DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
622
623     if (!decl_origin.Valid())
624       return false;
625
626     MinionSP minion_sp(GetMinion(&tag_decl->getASTContext(), decl_origin.ctx));
627
628     TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
629
630     for (Decl *origin_child_decl : origin_tag_decl->decls()) {
631       minion_sp->Import(origin_child_decl);
632     }
633
634     if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl)) {
635       record_decl->setHasLoadedFieldsFromExternalStorage(true);
636     }
637
638     return true;
639   }
640
641   if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
642     if (ObjCInterfaceDecl *objc_interface_decl =
643             objc_object_type->getInterface()) {
644       DeclOrigin decl_origin = GetDeclOrigin(objc_interface_decl);
645
646       if (!decl_origin.Valid())
647         return false;
648
649       MinionSP minion_sp(
650           GetMinion(&objc_interface_decl->getASTContext(), decl_origin.ctx));
651
652       ObjCInterfaceDecl *origin_interface_decl =
653           llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
654
655       for (Decl *origin_child_decl : origin_interface_decl->decls()) {
656         minion_sp->Import(origin_child_decl);
657       }
658
659       return true;
660     } else {
661       return false;
662     }
663   }
664
665   return true;
666 }
667
668 bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
669   if (type.isNull())
670     return false;
671
672   if (const TagType *tag_type = type->getAs<TagType>()) {
673     TagDecl *tag_decl = tag_type->getDecl();
674
675     if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
676       return true;
677
678     return CompleteTagDecl(tag_decl);
679   }
680   if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
681     if (ObjCInterfaceDecl *objc_interface_decl =
682             objc_object_type->getInterface())
683       return CompleteObjCInterfaceDecl(objc_interface_decl);
684     else
685       return false;
686   }
687   if (const ArrayType *array_type = type->getAsArrayTypeUnsafe()) {
688     return RequireCompleteType(array_type->getElementType());
689   }
690   if (const AtomicType *atomic_type = type->getAs<AtomicType>()) {
691     return RequireCompleteType(atomic_type->getPointeeType());
692   }
693
694   return true;
695 }
696
697 ClangASTMetadata *ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
698   DeclOrigin decl_origin = GetDeclOrigin(decl);
699
700   if (decl_origin.Valid())
701     return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
702   else
703     return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
704 }
705
706 ClangASTImporter::DeclOrigin
707 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) {
708   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
709
710   OriginMap &origins = context_md->m_origins;
711
712   OriginMap::iterator iter = origins.find(decl);
713
714   if (iter != origins.end())
715     return iter->second;
716   else
717     return DeclOrigin();
718 }
719
720 void ClangASTImporter::SetDeclOrigin(const clang::Decl *decl,
721                                      clang::Decl *original_decl) {
722   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
723
724   OriginMap &origins = context_md->m_origins;
725
726   OriginMap::iterator iter = origins.find(decl);
727
728   if (iter != origins.end()) {
729     iter->second.decl = original_decl;
730     iter->second.ctx = &original_decl->getASTContext();
731   } else {
732     origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
733   }
734 }
735
736 void ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
737                                             NamespaceMapSP &namespace_map) {
738   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
739
740   context_md->m_namespace_maps[decl] = namespace_map;
741 }
742
743 ClangASTImporter::NamespaceMapSP
744 ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl) {
745   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
746
747   NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
748
749   NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
750
751   if (iter != namespace_maps.end())
752     return iter->second;
753   else
754     return NamespaceMapSP();
755 }
756
757 void ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl) {
758   assert(decl);
759   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
760
761   const DeclContext *parent_context = decl->getDeclContext();
762   const NamespaceDecl *parent_namespace =
763       dyn_cast<NamespaceDecl>(parent_context);
764   NamespaceMapSP parent_map;
765
766   if (parent_namespace)
767     parent_map = GetNamespaceMap(parent_namespace);
768
769   NamespaceMapSP new_map;
770
771   new_map.reset(new NamespaceMap);
772
773   if (context_md->m_map_completer) {
774     std::string namespace_string = decl->getDeclName().getAsString();
775
776     context_md->m_map_completer->CompleteNamespaceMap(
777         new_map, ConstString(namespace_string.c_str()), parent_map);
778   }
779
780   context_md->m_namespace_maps[decl] = new_map;
781 }
782
783 void ClangASTImporter::ForgetDestination(clang::ASTContext *dst_ast) {
784   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
785
786   if (log)
787     log->Printf("    [ClangASTImporter] Forgetting destination (ASTContext*)%p",
788                 static_cast<void *>(dst_ast));
789
790   m_metadata_map.erase(dst_ast);
791 }
792
793 void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,
794                                     clang::ASTContext *src_ast) {
795   ASTContextMetadataSP md = MaybeGetContextMetadata(dst_ast);
796
797   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
798
799   if (log)
800     log->Printf("    [ClangASTImporter] Forgetting source->dest "
801                 "(ASTContext*)%p->(ASTContext*)%p",
802                 static_cast<void *>(src_ast), static_cast<void *>(dst_ast));
803
804   if (!md)
805     return;
806
807   md->m_minions.erase(src_ast);
808
809   for (OriginMap::iterator iter = md->m_origins.begin();
810        iter != md->m_origins.end();) {
811     if (iter->second.ctx == src_ast)
812       md->m_origins.erase(iter++);
813     else
814       ++iter;
815   }
816 }
817
818 ClangASTImporter::MapCompleter::~MapCompleter() { return; }
819
820 void ClangASTImporter::Minion::InitDeportWorkQueues(
821     std::set<clang::NamedDecl *> *decls_to_deport,
822     std::set<clang::NamedDecl *> *decls_already_deported) {
823   assert(!m_decls_to_deport);
824   assert(!m_decls_already_deported);
825
826   m_decls_to_deport = decls_to_deport;
827   m_decls_already_deported = decls_already_deported;
828 }
829
830 void ClangASTImporter::Minion::ExecuteDeportWorkQueues() {
831   assert(m_decls_to_deport);
832   assert(m_decls_already_deported);
833
834   ASTContextMetadataSP to_context_md =
835       m_master.GetContextMetadata(&getToContext());
836
837   while (!m_decls_to_deport->empty()) {
838     NamedDecl *decl = *m_decls_to_deport->begin();
839
840     m_decls_already_deported->insert(decl);
841     m_decls_to_deport->erase(decl);
842
843     DeclOrigin &origin = to_context_md->m_origins[decl];
844     UNUSED_IF_ASSERT_DISABLED(origin);
845
846     assert(origin.ctx ==
847            m_source_ctx); // otherwise we should never have added this
848                           // because it doesn't need to be deported
849
850     Decl *original_decl = to_context_md->m_origins[decl].decl;
851
852     ClangASTContext::GetCompleteDecl(m_source_ctx, original_decl);
853
854     if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) {
855       if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
856         if (original_tag_decl->isCompleteDefinition()) {
857           ImportDefinitionTo(tag_decl, original_tag_decl);
858           tag_decl->setCompleteDefinition(true);
859         }
860       }
861
862       tag_decl->setHasExternalLexicalStorage(false);
863       tag_decl->setHasExternalVisibleStorage(false);
864     } else if (ObjCContainerDecl *container_decl =
865                    dyn_cast<ObjCContainerDecl>(decl)) {
866       container_decl->setHasExternalLexicalStorage(false);
867       container_decl->setHasExternalVisibleStorage(false);
868     }
869
870     to_context_md->m_origins.erase(decl);
871   }
872
873   m_decls_to_deport = nullptr;
874   m_decls_already_deported = nullptr;
875 }
876
877 void ClangASTImporter::Minion::ImportDefinitionTo(clang::Decl *to,
878                                                   clang::Decl *from) {
879   ASTImporter::Imported(from, to);
880
881   /*
882   if (to_objc_interface)
883       to_objc_interface->startDefinition();
884
885   CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
886
887   if (to_cxx_record)
888       to_cxx_record->startDefinition();
889   */
890
891   ImportDefinition(from);
892
893   if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
894     if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
895       to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
896     }
897   }
898
899   // If we're dealing with an Objective-C class, ensure that the inheritance has
900   // been set up correctly.  The ASTImporter may not do this correctly if the
901   // class was originally sourced from symbols.
902
903   if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
904     do {
905       ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
906
907       if (to_superclass)
908         break; // we're not going to override it if it's set
909
910       ObjCInterfaceDecl *from_objc_interface =
911           dyn_cast<ObjCInterfaceDecl>(from);
912
913       if (!from_objc_interface)
914         break;
915
916       ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
917
918       if (!from_superclass)
919         break;
920
921       Decl *imported_from_superclass_decl = Import(from_superclass);
922
923       if (!imported_from_superclass_decl)
924         break;
925
926       ObjCInterfaceDecl *imported_from_superclass =
927           dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
928
929       if (!imported_from_superclass)
930         break;
931
932       if (!to_objc_interface->hasDefinition())
933         to_objc_interface->startDefinition();
934
935       to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo(
936           m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
937     } while (0);
938   }
939 }
940
941 clang::Decl *ClangASTImporter::Minion::Imported(clang::Decl *from,
942                                                 clang::Decl *to) {
943   ClangASTMetrics::RegisterClangImport();
944
945   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
946
947   lldb::user_id_t user_id = LLDB_INVALID_UID;
948   ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
949   if (metadata)
950     user_id = metadata->GetUserID();
951
952   if (log) {
953     if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
954       std::string name_string;
955       llvm::raw_string_ostream name_stream(name_string);
956       from_named_decl->printName(name_stream);
957       name_stream.flush();
958
959       log->Printf("    [ClangASTImporter] Imported (%sDecl*)%p, named %s (from "
960                   "(Decl*)%p), metadata 0x%" PRIx64,
961                   from->getDeclKindName(), static_cast<void *>(to),
962                   name_string.c_str(), static_cast<void *>(from), user_id);
963     } else {
964       log->Printf("    [ClangASTImporter] Imported (%sDecl*)%p (from "
965                   "(Decl*)%p), metadata 0x%" PRIx64,
966                   from->getDeclKindName(), static_cast<void *>(to),
967                   static_cast<void *>(from), user_id);
968     }
969   }
970
971   ASTContextMetadataSP to_context_md =
972       m_master.GetContextMetadata(&to->getASTContext());
973   ASTContextMetadataSP from_context_md =
974       m_master.MaybeGetContextMetadata(m_source_ctx);
975
976   if (from_context_md) {
977     OriginMap &origins = from_context_md->m_origins;
978
979     OriginMap::iterator origin_iter = origins.find(from);
980
981     if (origin_iter != origins.end()) {
982       if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
983           user_id != LLDB_INVALID_UID) {
984         if (origin_iter->second.ctx != &to->getASTContext())
985           to_context_md->m_origins[to] = origin_iter->second;
986       }
987
988       MinionSP direct_completer =
989           m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
990
991       if (direct_completer.get() != this)
992         direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
993
994       if (log)
995         log->Printf("    [ClangASTImporter] Propagated origin "
996                     "(Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to "
997                     "(ASTContext*)%p",
998                     static_cast<void *>(origin_iter->second.decl),
999                     static_cast<void *>(origin_iter->second.ctx),
1000                     static_cast<void *>(&from->getASTContext()),
1001                     static_cast<void *>(&to->getASTContext()));
1002     } else {
1003       if (m_decls_to_deport && m_decls_already_deported) {
1004         if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to)) {
1005           RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
1006           if (from_record_decl == nullptr ||
1007               from_record_decl->isInjectedClassName() == false) {
1008             NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
1009
1010             if (!m_decls_already_deported->count(to_named_decl))
1011               m_decls_to_deport->insert(to_named_decl);
1012           }
1013         }
1014       }
1015
1016       if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
1017           user_id != LLDB_INVALID_UID) {
1018         to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1019       }
1020
1021       if (log)
1022         log->Printf("    [ClangASTImporter] Decl has no origin information in "
1023                     "(ASTContext*)%p",
1024                     static_cast<void *>(&from->getASTContext()));
1025     }
1026
1027     if (clang::NamespaceDecl *to_namespace =
1028             dyn_cast<clang::NamespaceDecl>(to)) {
1029       clang::NamespaceDecl *from_namespace =
1030           dyn_cast<clang::NamespaceDecl>(from);
1031
1032       NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
1033
1034       NamespaceMetaMap::iterator namespace_map_iter =
1035           namespace_maps.find(from_namespace);
1036
1037       if (namespace_map_iter != namespace_maps.end())
1038         to_context_md->m_namespace_maps[to_namespace] =
1039             namespace_map_iter->second;
1040     }
1041   } else {
1042     to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1043
1044     if (log)
1045       log->Printf("    [ClangASTImporter] Sourced origin "
1046                   "(Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
1047                   static_cast<void *>(from), static_cast<void *>(m_source_ctx),
1048                   static_cast<void *>(&to->getASTContext()));
1049   }
1050
1051   if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from)) {
1052     TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
1053
1054     to_tag_decl->setHasExternalLexicalStorage();
1055     to_tag_decl->setMustBuildLookupTable();
1056
1057     if (log)
1058       log->Printf(
1059           "    [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
1060           (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1061           (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1062           (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
1063           (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
1064   }
1065
1066   if (isa<NamespaceDecl>(from)) {
1067     NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
1068
1069     m_master.BuildNamespaceMap(to_namespace_decl);
1070
1071     to_namespace_decl->setHasExternalVisibleStorage();
1072   }
1073
1074   if (isa<ObjCContainerDecl>(from)) {
1075     ObjCContainerDecl *to_container_decl = dyn_cast<ObjCContainerDecl>(to);
1076
1077     to_container_decl->setHasExternalLexicalStorage();
1078     to_container_decl->setHasExternalVisibleStorage();
1079
1080     /*to_interface_decl->setExternallyCompleted();*/
1081
1082     if (log) {
1083       if (ObjCInterfaceDecl *to_interface_decl =
1084               llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1085         log->Printf(
1086             "    [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1087             "%s%s%s",
1088             (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1089             (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1090             (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
1091       } else {
1092         log->Printf(
1093             "    [ClangASTImporter] To is an %sDecl - attributes %s%s",
1094             ((Decl *)to_container_decl)->getDeclKindName(),
1095             (to_container_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1096             (to_container_decl->hasExternalVisibleStorage() ? " Visible" : ""));
1097       }
1098     }
1099   }
1100
1101   return clang::ASTImporter::Imported(from, to);
1102 }
1103
1104 clang::Decl *ClangASTImporter::Minion::GetOriginalDecl(clang::Decl *To) {
1105   ASTContextMetadataSP to_context_md =
1106       m_master.GetContextMetadata(&To->getASTContext());
1107
1108   if (!to_context_md)
1109     return nullptr;
1110
1111   OriginMap::iterator iter = to_context_md->m_origins.find(To);
1112
1113   if (iter == to_context_md->m_origins.end())
1114     return nullptr;
1115
1116   return const_cast<clang::Decl *>(iter->second.decl);
1117 }