• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- ClangASTImporter.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Core/Module.h"
10 #include "lldb/Utility/LLDBAssert.h"
11 #include "lldb/Utility/Log.h"
12 #include "clang/AST/Decl.h"
13 #include "clang/AST/DeclCXX.h"
14 #include "clang/AST/DeclObjC.h"
15 #include "clang/Sema/Lookup.h"
16 #include "clang/Sema/Sema.h"
17 #include "llvm/Support/raw_ostream.h"
18 
19 #include "Plugins/ExpressionParser/Clang/ClangASTImporter.h"
20 #include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h"
21 #include "Plugins/ExpressionParser/Clang/ClangASTSource.h"
22 #include "Plugins/ExpressionParser/Clang/ClangExternalASTSourceCallbacks.h"
23 #include "Plugins/ExpressionParser/Clang/ClangUtil.h"
24 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
25 
26 #include <memory>
27 
28 using namespace lldb_private;
29 using namespace clang;
30 
CopyType(TypeSystemClang & dst_ast,const CompilerType & src_type)31 CompilerType ClangASTImporter::CopyType(TypeSystemClang &dst_ast,
32                                         const CompilerType &src_type) {
33   clang::ASTContext &dst_clang_ast = dst_ast.getASTContext();
34 
35   TypeSystemClang *src_ast =
36       llvm::dyn_cast_or_null<TypeSystemClang>(src_type.GetTypeSystem());
37   if (!src_ast)
38     return CompilerType();
39 
40   clang::ASTContext &src_clang_ast = src_ast->getASTContext();
41 
42   clang::QualType src_qual_type = ClangUtil::GetQualType(src_type);
43 
44   ImporterDelegateSP delegate_sp(GetDelegate(&dst_clang_ast, &src_clang_ast));
45   if (!delegate_sp)
46     return CompilerType();
47 
48   ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, &dst_clang_ast);
49 
50   llvm::Expected<QualType> ret_or_error = delegate_sp->Import(src_qual_type);
51   if (!ret_or_error) {
52     Log *log =
53       lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
54     LLDB_LOG_ERROR(log, ret_or_error.takeError(),
55         "Couldn't import type: {0}");
56     return CompilerType();
57   }
58 
59   lldb::opaque_compiler_type_t dst_clang_type = ret_or_error->getAsOpaquePtr();
60 
61   if (dst_clang_type)
62     return CompilerType(&dst_ast, dst_clang_type);
63   return CompilerType();
64 }
65 
CopyDecl(clang::ASTContext * dst_ast,clang::Decl * decl)66 clang::Decl *ClangASTImporter::CopyDecl(clang::ASTContext *dst_ast,
67                                         clang::Decl *decl) {
68   ImporterDelegateSP delegate_sp;
69 
70   clang::ASTContext *src_ast = &decl->getASTContext();
71   delegate_sp = GetDelegate(dst_ast, src_ast);
72 
73   ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, dst_ast);
74 
75   if (!delegate_sp)
76     return nullptr;
77 
78   llvm::Expected<clang::Decl *> result = delegate_sp->Import(decl);
79   if (!result) {
80     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
81     LLDB_LOG_ERROR(log, result.takeError(), "Couldn't import decl: {0}");
82     if (log) {
83       lldb::user_id_t user_id = LLDB_INVALID_UID;
84       ClangASTMetadata *metadata = GetDeclMetadata(decl);
85       if (metadata)
86         user_id = metadata->GetUserID();
87 
88       if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
89         LLDB_LOG(log,
90                  "  [ClangASTImporter] WARNING: Failed to import a {0} "
91                  "'{1}', metadata {2}",
92                  decl->getDeclKindName(), named_decl->getNameAsString(),
93                  user_id);
94       else
95         LLDB_LOG(log,
96                  "  [ClangASTImporter] WARNING: Failed to import a {0}, "
97                  "metadata {1}",
98                  decl->getDeclKindName(), user_id);
99     }
100     return nullptr;
101   }
102 
103   return *result;
104 }
105 
106 class DeclContextOverride {
107 private:
108   struct Backup {
109     clang::DeclContext *decl_context;
110     clang::DeclContext *lexical_decl_context;
111   };
112 
113   llvm::DenseMap<clang::Decl *, Backup> m_backups;
114 
OverrideOne(clang::Decl * decl)115   void OverrideOne(clang::Decl *decl) {
116     if (m_backups.find(decl) != m_backups.end()) {
117       return;
118     }
119 
120     m_backups[decl] = {decl->getDeclContext(), decl->getLexicalDeclContext()};
121 
122     decl->setDeclContext(decl->getASTContext().getTranslationUnitDecl());
123     decl->setLexicalDeclContext(decl->getASTContext().getTranslationUnitDecl());
124   }
125 
ChainPassesThrough(clang::Decl * decl,clang::DeclContext * base,clang::DeclContext * (clang::Decl::* contextFromDecl)(),clang::DeclContext * (clang::DeclContext::* contextFromContext)())126   bool ChainPassesThrough(
127       clang::Decl *decl, clang::DeclContext *base,
128       clang::DeclContext *(clang::Decl::*contextFromDecl)(),
129       clang::DeclContext *(clang::DeclContext::*contextFromContext)()) {
130     for (DeclContext *decl_ctx = (decl->*contextFromDecl)(); decl_ctx;
131          decl_ctx = (decl_ctx->*contextFromContext)()) {
132       if (decl_ctx == base) {
133         return true;
134       }
135     }
136 
137     return false;
138   }
139 
GetEscapedChild(clang::Decl * decl,clang::DeclContext * base=nullptr)140   clang::Decl *GetEscapedChild(clang::Decl *decl,
141                                clang::DeclContext *base = nullptr) {
142     if (base) {
143       // decl's DeclContext chains must pass through base.
144 
145       if (!ChainPassesThrough(decl, base, &clang::Decl::getDeclContext,
146                               &clang::DeclContext::getParent) ||
147           !ChainPassesThrough(decl, base, &clang::Decl::getLexicalDeclContext,
148                               &clang::DeclContext::getLexicalParent)) {
149         return decl;
150       }
151     } else {
152       base = clang::dyn_cast<clang::DeclContext>(decl);
153 
154       if (!base) {
155         return nullptr;
156       }
157     }
158 
159     if (clang::DeclContext *context =
160             clang::dyn_cast<clang::DeclContext>(decl)) {
161       for (clang::Decl *decl : context->decls()) {
162         if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
163           return escaped_child;
164         }
165       }
166     }
167 
168     return nullptr;
169   }
170 
Override(clang::Decl * decl)171   void Override(clang::Decl *decl) {
172     if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
173       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
174 
175       LLDB_LOG(log,
176                "    [ClangASTImporter] DeclContextOverride couldn't "
177                "override ({0}Decl*){1} - its child ({2}Decl*){3} escapes",
178                decl->getDeclKindName(), decl, escaped_child->getDeclKindName(),
179                escaped_child);
180       lldbassert(0 && "Couldn't override!");
181     }
182 
183     OverrideOne(decl);
184   }
185 
186 public:
DeclContextOverride()187   DeclContextOverride() {}
188 
OverrideAllDeclsFromContainingFunction(clang::Decl * decl)189   void OverrideAllDeclsFromContainingFunction(clang::Decl *decl) {
190     for (DeclContext *decl_context = decl->getLexicalDeclContext();
191          decl_context; decl_context = decl_context->getLexicalParent()) {
192       DeclContext *redecl_context = decl_context->getRedeclContext();
193 
194       if (llvm::isa<FunctionDecl>(redecl_context) &&
195           llvm::isa<TranslationUnitDecl>(redecl_context->getLexicalParent())) {
196         for (clang::Decl *child_decl : decl_context->decls()) {
197           Override(child_decl);
198         }
199       }
200     }
201   }
202 
~DeclContextOverride()203   ~DeclContextOverride() {
204     for (const std::pair<clang::Decl *, Backup> &backup : m_backups) {
205       backup.first->setDeclContext(backup.second.decl_context);
206       backup.first->setLexicalDeclContext(backup.second.lexical_decl_context);
207     }
208   }
209 };
210 
211 namespace {
212 /// Completes all imported TagDecls at the end of the scope.
213 ///
214 /// While in a CompleteTagDeclsScope, every decl that could be completed will
215 /// be completed at the end of the scope (including all Decls that are
216 /// imported while completing the original Decls).
217 class CompleteTagDeclsScope : public ClangASTImporter::NewDeclListener {
218   ClangASTImporter::ImporterDelegateSP m_delegate;
219   /// List of declarations in the target context that need to be completed.
220   /// Every declaration should only be completed once and therefore should only
221   /// be once in this list.
222   llvm::SetVector<NamedDecl *> m_decls_to_complete;
223   /// Set of declarations that already were successfully completed (not just
224   /// added to m_decls_to_complete).
225   llvm::SmallPtrSet<NamedDecl *, 32> m_decls_already_completed;
226   clang::ASTContext *m_dst_ctx;
227   clang::ASTContext *m_src_ctx;
228   ClangASTImporter &importer;
229 
230 public:
231   /// Constructs a CompleteTagDeclsScope.
232   /// \param importer The ClangASTImporter that we should observe.
233   /// \param dst_ctx The ASTContext to which Decls are imported.
234   /// \param src_ctx The ASTContext from which Decls are imported.
CompleteTagDeclsScope(ClangASTImporter & importer,clang::ASTContext * dst_ctx,clang::ASTContext * src_ctx)235   explicit CompleteTagDeclsScope(ClangASTImporter &importer,
236                             clang::ASTContext *dst_ctx,
237                             clang::ASTContext *src_ctx)
238       : m_delegate(importer.GetDelegate(dst_ctx, src_ctx)), m_dst_ctx(dst_ctx),
239         m_src_ctx(src_ctx), importer(importer) {
240     m_delegate->SetImportListener(this);
241   }
242 
~CompleteTagDeclsScope()243   virtual ~CompleteTagDeclsScope() {
244     ClangASTImporter::ASTContextMetadataSP to_context_md =
245         importer.GetContextMetadata(m_dst_ctx);
246 
247     // Complete all decls we collected until now.
248     while (!m_decls_to_complete.empty()) {
249       NamedDecl *decl = m_decls_to_complete.pop_back_val();
250       m_decls_already_completed.insert(decl);
251 
252       // The decl that should be completed has to be imported into the target
253       // context from some other context.
254       assert(to_context_md->hasOrigin(decl));
255       // We should only complete decls coming from the source context.
256       assert(to_context_md->getOrigin(decl).ctx == m_src_ctx);
257 
258       Decl *original_decl = to_context_md->getOrigin(decl).decl;
259 
260       // Complete the decl now.
261       TypeSystemClang::GetCompleteDecl(m_src_ctx, original_decl);
262       if (auto *tag_decl = dyn_cast<TagDecl>(decl)) {
263         if (auto *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
264           if (original_tag_decl->isCompleteDefinition()) {
265             m_delegate->ImportDefinitionTo(tag_decl, original_tag_decl);
266             tag_decl->setCompleteDefinition(true);
267           }
268         }
269 
270         tag_decl->setHasExternalLexicalStorage(false);
271         tag_decl->setHasExternalVisibleStorage(false);
272       } else if (auto *container_decl = dyn_cast<ObjCContainerDecl>(decl)) {
273         container_decl->setHasExternalLexicalStorage(false);
274         container_decl->setHasExternalVisibleStorage(false);
275       }
276 
277       to_context_md->removeOrigin(decl);
278     }
279 
280     // Stop listening to imported decls. We do this after clearing the
281     // Decls we needed to import to catch all Decls they might have pulled in.
282     m_delegate->RemoveImportListener();
283   }
284 
NewDeclImported(clang::Decl * from,clang::Decl * to)285   void NewDeclImported(clang::Decl *from, clang::Decl *to) override {
286     // Filter out decls that we can't complete later.
287     if (!isa<TagDecl>(to) && !isa<ObjCInterfaceDecl>(to))
288       return;
289     RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
290     // We don't need to complete injected class name decls.
291     if (from_record_decl && from_record_decl->isInjectedClassName())
292       return;
293 
294     NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
295     // Check if we already completed this type.
296     if (m_decls_already_completed.count(to_named_decl) != 0)
297       return;
298     // Queue this type to be completed.
299     m_decls_to_complete.insert(to_named_decl);
300   }
301 };
302 } // namespace
303 
DeportType(TypeSystemClang & dst,const CompilerType & src_type)304 CompilerType ClangASTImporter::DeportType(TypeSystemClang &dst,
305                                           const CompilerType &src_type) {
306   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
307 
308   TypeSystemClang *src_ctxt =
309       llvm::cast<TypeSystemClang>(src_type.GetTypeSystem());
310 
311   LLDB_LOG(log,
312            "    [ClangASTImporter] DeportType called on ({0}Type*){1} "
313            "from (ASTContext*){2} to (ASTContext*){3}",
314            src_type.GetTypeName(), src_type.GetOpaqueQualType(),
315            &src_ctxt->getASTContext(), &dst.getASTContext());
316 
317   DeclContextOverride decl_context_override;
318 
319   if (auto *t = ClangUtil::GetQualType(src_type)->getAs<TagType>())
320     decl_context_override.OverrideAllDeclsFromContainingFunction(t->getDecl());
321 
322   CompleteTagDeclsScope complete_scope(*this, &dst.getASTContext(),
323                                        &src_ctxt->getASTContext());
324   return CopyType(dst, src_type);
325 }
326 
DeportDecl(clang::ASTContext * dst_ctx,clang::Decl * decl)327 clang::Decl *ClangASTImporter::DeportDecl(clang::ASTContext *dst_ctx,
328                                           clang::Decl *decl) {
329   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
330 
331   clang::ASTContext *src_ctx = &decl->getASTContext();
332   LLDB_LOG(log,
333            "    [ClangASTImporter] DeportDecl called on ({0}Decl*){1} from "
334            "(ASTContext*){2} to (ASTContext*){3}",
335            decl->getDeclKindName(), decl, src_ctx, dst_ctx);
336 
337   DeclContextOverride decl_context_override;
338 
339   decl_context_override.OverrideAllDeclsFromContainingFunction(decl);
340 
341   clang::Decl *result;
342   {
343     CompleteTagDeclsScope complete_scope(*this, dst_ctx, src_ctx);
344     result = CopyDecl(dst_ctx, decl);
345   }
346 
347   if (!result)
348     return nullptr;
349 
350   LLDB_LOG(log,
351            "    [ClangASTImporter] DeportDecl deported ({0}Decl*){1} to "
352            "({2}Decl*){3}",
353            decl->getDeclKindName(), decl, result->getDeclKindName(), result);
354 
355   return result;
356 }
357 
CanImport(const CompilerType & type)358 bool ClangASTImporter::CanImport(const CompilerType &type) {
359   if (!ClangUtil::IsClangType(type))
360     return false;
361 
362   // TODO: remove external completion BOOL
363   // CompleteAndFetchChildren should get the Decl out and check for the
364 
365   clang::QualType qual_type(
366       ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
367 
368   const clang::Type::TypeClass type_class = qual_type->getTypeClass();
369   switch (type_class) {
370   case clang::Type::Record: {
371     const clang::CXXRecordDecl *cxx_record_decl =
372         qual_type->getAsCXXRecordDecl();
373     if (cxx_record_decl) {
374       if (GetDeclOrigin(cxx_record_decl).Valid())
375         return true;
376     }
377   } break;
378 
379   case clang::Type::Enum: {
380     clang::EnumDecl *enum_decl =
381         llvm::cast<clang::EnumType>(qual_type)->getDecl();
382     if (enum_decl) {
383       if (GetDeclOrigin(enum_decl).Valid())
384         return true;
385     }
386   } break;
387 
388   case clang::Type::ObjCObject:
389   case clang::Type::ObjCInterface: {
390     const clang::ObjCObjectType *objc_class_type =
391         llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
392     if (objc_class_type) {
393       clang::ObjCInterfaceDecl *class_interface_decl =
394           objc_class_type->getInterface();
395       // We currently can't complete objective C types through the newly added
396       // ASTContext because it only supports TagDecl objects right now...
397       if (class_interface_decl) {
398         if (GetDeclOrigin(class_interface_decl).Valid())
399           return true;
400       }
401     }
402   } break;
403 
404   case clang::Type::Typedef:
405     return CanImport(CompilerType(type.GetTypeSystem(),
406                                   llvm::cast<clang::TypedefType>(qual_type)
407                                       ->getDecl()
408                                       ->getUnderlyingType()
409                                       .getAsOpaquePtr()));
410 
411   case clang::Type::Auto:
412     return CanImport(CompilerType(type.GetTypeSystem(),
413                                   llvm::cast<clang::AutoType>(qual_type)
414                                       ->getDeducedType()
415                                       .getAsOpaquePtr()));
416 
417   case clang::Type::Elaborated:
418     return CanImport(CompilerType(type.GetTypeSystem(),
419                                   llvm::cast<clang::ElaboratedType>(qual_type)
420                                       ->getNamedType()
421                                       .getAsOpaquePtr()));
422 
423   case clang::Type::Paren:
424     return CanImport(CompilerType(
425         type.GetTypeSystem(),
426         llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
427 
428   default:
429     break;
430   }
431 
432   return false;
433 }
434 
Import(const CompilerType & type)435 bool ClangASTImporter::Import(const CompilerType &type) {
436   if (!ClangUtil::IsClangType(type))
437     return false;
438   // TODO: remove external completion BOOL
439   // CompleteAndFetchChildren should get the Decl out and check for the
440 
441   clang::QualType qual_type(
442       ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
443 
444   const clang::Type::TypeClass type_class = qual_type->getTypeClass();
445   switch (type_class) {
446   case clang::Type::Record: {
447     const clang::CXXRecordDecl *cxx_record_decl =
448         qual_type->getAsCXXRecordDecl();
449     if (cxx_record_decl) {
450       if (GetDeclOrigin(cxx_record_decl).Valid())
451         return CompleteAndFetchChildren(qual_type);
452     }
453   } break;
454 
455   case clang::Type::Enum: {
456     clang::EnumDecl *enum_decl =
457         llvm::cast<clang::EnumType>(qual_type)->getDecl();
458     if (enum_decl) {
459       if (GetDeclOrigin(enum_decl).Valid())
460         return CompleteAndFetchChildren(qual_type);
461     }
462   } break;
463 
464   case clang::Type::ObjCObject:
465   case clang::Type::ObjCInterface: {
466     const clang::ObjCObjectType *objc_class_type =
467         llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
468     if (objc_class_type) {
469       clang::ObjCInterfaceDecl *class_interface_decl =
470           objc_class_type->getInterface();
471       // We currently can't complete objective C types through the newly added
472       // ASTContext because it only supports TagDecl objects right now...
473       if (class_interface_decl) {
474         if (GetDeclOrigin(class_interface_decl).Valid())
475           return CompleteAndFetchChildren(qual_type);
476       }
477     }
478   } break;
479 
480   case clang::Type::Typedef:
481     return Import(CompilerType(type.GetTypeSystem(),
482                                llvm::cast<clang::TypedefType>(qual_type)
483                                    ->getDecl()
484                                    ->getUnderlyingType()
485                                    .getAsOpaquePtr()));
486 
487   case clang::Type::Auto:
488     return Import(CompilerType(type.GetTypeSystem(),
489                                llvm::cast<clang::AutoType>(qual_type)
490                                    ->getDeducedType()
491                                    .getAsOpaquePtr()));
492 
493   case clang::Type::Elaborated:
494     return Import(CompilerType(type.GetTypeSystem(),
495                                llvm::cast<clang::ElaboratedType>(qual_type)
496                                    ->getNamedType()
497                                    .getAsOpaquePtr()));
498 
499   case clang::Type::Paren:
500     return Import(CompilerType(
501         type.GetTypeSystem(),
502         llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
503 
504   default:
505     break;
506   }
507   return false;
508 }
509 
CompleteType(const CompilerType & compiler_type)510 bool ClangASTImporter::CompleteType(const CompilerType &compiler_type) {
511   if (!CanImport(compiler_type))
512     return false;
513 
514   if (Import(compiler_type)) {
515     TypeSystemClang::CompleteTagDeclarationDefinition(compiler_type);
516     return true;
517   }
518 
519   TypeSystemClang::SetHasExternalStorage(compiler_type.GetOpaqueQualType(),
520                                          false);
521   return false;
522 }
523 
LayoutRecordType(const clang::RecordDecl * record_decl,uint64_t & bit_size,uint64_t & alignment,llvm::DenseMap<const clang::FieldDecl *,uint64_t> & field_offsets,llvm::DenseMap<const clang::CXXRecordDecl *,clang::CharUnits> & base_offsets,llvm::DenseMap<const clang::CXXRecordDecl *,clang::CharUnits> & vbase_offsets)524 bool ClangASTImporter::LayoutRecordType(
525     const clang::RecordDecl *record_decl, uint64_t &bit_size,
526     uint64_t &alignment,
527     llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
528     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
529         &base_offsets,
530     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
531         &vbase_offsets) {
532   RecordDeclToLayoutMap::iterator pos =
533       m_record_decl_to_layout_map.find(record_decl);
534   bool success = false;
535   base_offsets.clear();
536   vbase_offsets.clear();
537   if (pos != m_record_decl_to_layout_map.end()) {
538     bit_size = pos->second.bit_size;
539     alignment = pos->second.alignment;
540     field_offsets.swap(pos->second.field_offsets);
541     base_offsets.swap(pos->second.base_offsets);
542     vbase_offsets.swap(pos->second.vbase_offsets);
543     m_record_decl_to_layout_map.erase(pos);
544     success = true;
545   } else {
546     bit_size = 0;
547     alignment = 0;
548     field_offsets.clear();
549   }
550   return success;
551 }
552 
SetRecordLayout(clang::RecordDecl * decl,const LayoutInfo & layout)553 void ClangASTImporter::SetRecordLayout(clang::RecordDecl *decl,
554                                         const LayoutInfo &layout) {
555   m_record_decl_to_layout_map.insert(std::make_pair(decl, layout));
556 }
557 
CompleteTagDecl(clang::TagDecl * decl)558 bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
559   DeclOrigin decl_origin = GetDeclOrigin(decl);
560 
561   if (!decl_origin.Valid())
562     return false;
563 
564   if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
565     return false;
566 
567   ImporterDelegateSP delegate_sp(
568       GetDelegate(&decl->getASTContext(), decl_origin.ctx));
569 
570   ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
571                                                 &decl->getASTContext());
572   if (delegate_sp)
573     delegate_sp->ImportDefinitionTo(decl, decl_origin.decl);
574 
575   return true;
576 }
577 
CompleteTagDeclWithOrigin(clang::TagDecl * decl,clang::TagDecl * origin_decl)578 bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl,
579                                                  clang::TagDecl *origin_decl) {
580   clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
581 
582   if (!TypeSystemClang::GetCompleteDecl(origin_ast_ctx, origin_decl))
583     return false;
584 
585   ImporterDelegateSP delegate_sp(
586       GetDelegate(&decl->getASTContext(), origin_ast_ctx));
587 
588   if (delegate_sp)
589     delegate_sp->ImportDefinitionTo(decl, origin_decl);
590 
591   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
592 
593   context_md->setOrigin(decl, DeclOrigin(origin_ast_ctx, origin_decl));
594   return true;
595 }
596 
CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl * interface_decl)597 bool ClangASTImporter::CompleteObjCInterfaceDecl(
598     clang::ObjCInterfaceDecl *interface_decl) {
599   DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
600 
601   if (!decl_origin.Valid())
602     return false;
603 
604   if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
605     return false;
606 
607   ImporterDelegateSP delegate_sp(
608       GetDelegate(&interface_decl->getASTContext(), decl_origin.ctx));
609 
610   if (delegate_sp)
611     delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
612 
613   if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
614     RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
615 
616   return true;
617 }
618 
CompleteAndFetchChildren(clang::QualType type)619 bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
620   if (!RequireCompleteType(type))
621     return false;
622 
623   Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
624 
625   if (const TagType *tag_type = type->getAs<TagType>()) {
626     TagDecl *tag_decl = tag_type->getDecl();
627 
628     DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
629 
630     if (!decl_origin.Valid())
631       return false;
632 
633     ImporterDelegateSP delegate_sp(
634         GetDelegate(&tag_decl->getASTContext(), decl_origin.ctx));
635 
636     ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
637                                                   &tag_decl->getASTContext());
638 
639     TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
640 
641     for (Decl *origin_child_decl : origin_tag_decl->decls()) {
642       llvm::Expected<Decl *> imported_or_err =
643           delegate_sp->Import(origin_child_decl);
644       if (!imported_or_err) {
645         LLDB_LOG_ERROR(log, imported_or_err.takeError(),
646                        "Couldn't import decl: {0}");
647         return false;
648       }
649     }
650 
651     if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl))
652       record_decl->setHasLoadedFieldsFromExternalStorage(true);
653 
654     return true;
655   }
656 
657   if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
658     if (ObjCInterfaceDecl *objc_interface_decl =
659             objc_object_type->getInterface()) {
660       DeclOrigin decl_origin = GetDeclOrigin(objc_interface_decl);
661 
662       if (!decl_origin.Valid())
663         return false;
664 
665       ImporterDelegateSP delegate_sp(
666           GetDelegate(&objc_interface_decl->getASTContext(), decl_origin.ctx));
667 
668       ObjCInterfaceDecl *origin_interface_decl =
669           llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
670 
671       for (Decl *origin_child_decl : origin_interface_decl->decls()) {
672         llvm::Expected<Decl *> imported_or_err =
673             delegate_sp->Import(origin_child_decl);
674         if (!imported_or_err) {
675           LLDB_LOG_ERROR(log, imported_or_err.takeError(),
676                          "Couldn't import decl: {0}");
677           return false;
678         }
679       }
680 
681       return true;
682     }
683     return false;
684   }
685 
686   return true;
687 }
688 
RequireCompleteType(clang::QualType type)689 bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
690   if (type.isNull())
691     return false;
692 
693   if (const TagType *tag_type = type->getAs<TagType>()) {
694     TagDecl *tag_decl = tag_type->getDecl();
695 
696     if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
697       return true;
698 
699     return CompleteTagDecl(tag_decl);
700   }
701   if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
702     if (ObjCInterfaceDecl *objc_interface_decl =
703             objc_object_type->getInterface())
704       return CompleteObjCInterfaceDecl(objc_interface_decl);
705     return false;
706   }
707   if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
708     return RequireCompleteType(array_type->getElementType());
709   if (const AtomicType *atomic_type = type->getAs<AtomicType>())
710     return RequireCompleteType(atomic_type->getPointeeType());
711 
712   return true;
713 }
714 
GetDeclMetadata(const clang::Decl * decl)715 ClangASTMetadata *ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
716   DeclOrigin decl_origin = GetDeclOrigin(decl);
717 
718   if (decl_origin.Valid()) {
719     TypeSystemClang *ast = TypeSystemClang::GetASTContext(decl_origin.ctx);
720     return ast->GetMetadata(decl_origin.decl);
721   }
722   TypeSystemClang *ast = TypeSystemClang::GetASTContext(&decl->getASTContext());
723   return ast->GetMetadata(decl);
724 }
725 
726 ClangASTImporter::DeclOrigin
GetDeclOrigin(const clang::Decl * decl)727 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) {
728   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
729 
730   return context_md->getOrigin(decl);
731 }
732 
SetDeclOrigin(const clang::Decl * decl,clang::Decl * original_decl)733 void ClangASTImporter::SetDeclOrigin(const clang::Decl *decl,
734                                      clang::Decl *original_decl) {
735   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
736   context_md->setOrigin(
737       decl, DeclOrigin(&original_decl->getASTContext(), original_decl));
738 }
739 
RegisterNamespaceMap(const clang::NamespaceDecl * decl,NamespaceMapSP & namespace_map)740 void ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
741                                             NamespaceMapSP &namespace_map) {
742   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
743 
744   context_md->m_namespace_maps[decl] = namespace_map;
745 }
746 
747 ClangASTImporter::NamespaceMapSP
GetNamespaceMap(const clang::NamespaceDecl * decl)748 ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl) {
749   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
750 
751   NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
752 
753   NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
754 
755   if (iter != namespace_maps.end())
756     return iter->second;
757   return NamespaceMapSP();
758 }
759 
BuildNamespaceMap(const clang::NamespaceDecl * decl)760 void ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl) {
761   assert(decl);
762   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
763 
764   const DeclContext *parent_context = decl->getDeclContext();
765   const NamespaceDecl *parent_namespace =
766       dyn_cast<NamespaceDecl>(parent_context);
767   NamespaceMapSP parent_map;
768 
769   if (parent_namespace)
770     parent_map = GetNamespaceMap(parent_namespace);
771 
772   NamespaceMapSP new_map;
773 
774   new_map = std::make_shared<NamespaceMap>();
775 
776   if (context_md->m_map_completer) {
777     std::string namespace_string = decl->getDeclName().getAsString();
778 
779     context_md->m_map_completer->CompleteNamespaceMap(
780         new_map, ConstString(namespace_string.c_str()), parent_map);
781   }
782 
783   context_md->m_namespace_maps[decl] = new_map;
784 }
785 
ForgetDestination(clang::ASTContext * dst_ast)786 void ClangASTImporter::ForgetDestination(clang::ASTContext *dst_ast) {
787   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
788 
789   LLDB_LOG(log,
790            "    [ClangASTImporter] Forgetting destination (ASTContext*){0}",
791            dst_ast);
792 
793   m_metadata_map.erase(dst_ast);
794 }
795 
ForgetSource(clang::ASTContext * dst_ast,clang::ASTContext * src_ast)796 void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,
797                                     clang::ASTContext *src_ast) {
798   ASTContextMetadataSP md = MaybeGetContextMetadata(dst_ast);
799 
800   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
801 
802   LLDB_LOG(log,
803            "    [ClangASTImporter] Forgetting source->dest "
804            "(ASTContext*){0}->(ASTContext*){1}",
805            src_ast, dst_ast);
806 
807   if (!md)
808     return;
809 
810   md->m_delegates.erase(src_ast);
811   md->removeOriginsWithContext(src_ast);
812 }
813 
~MapCompleter()814 ClangASTImporter::MapCompleter::~MapCompleter() { return; }
815 
816 llvm::Expected<Decl *>
ImportImpl(Decl * From)817 ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {
818   if (m_std_handler) {
819     llvm::Optional<Decl *> D = m_std_handler->Import(From);
820     if (D) {
821       // Make sure we don't use this decl later to map it back to it's original
822       // decl. The decl the CxxModuleHandler created has nothing to do with
823       // the one from debug info, and linking those two would just cause the
824       // ASTImporter to try 'updating' the module decl with the minimal one from
825       // the debug info.
826       m_decls_to_ignore.insert(*D);
827       return *D;
828     }
829   }
830 
831   // Check which ASTContext this declaration originally came from.
832   DeclOrigin origin = m_master.GetDeclOrigin(From);
833   // If it originally came from the target ASTContext then we can just
834   // pretend that the original is the one we imported. This can happen for
835   // example when inspecting a persistent declaration from the scratch
836   // ASTContext (which will provide the declaration when parsing the
837   // expression and then we later try to copy the declaration back to the
838   // scratch ASTContext to store the result).
839   // Without this check we would ask the ASTImporter to import a declaration
840   // into the same ASTContext where it came from (which doesn't make a lot of
841   // sense).
842   if (origin.Valid() && origin.ctx == &getToContext()) {
843     RegisterImportedDecl(From, origin.decl);
844     return origin.decl;
845   }
846 
847   // This declaration came originally from another ASTContext. Instead of
848   // copying our potentially incomplete 'From' Decl we instead go to the
849   // original ASTContext and copy the original to the target. This is not
850   // only faster than first completing our current decl and then copying it
851   // to the target, but it also prevents that indirectly copying the same
852   // declaration to the same target requires the ASTImporter to merge all
853   // the different decls that appear to come from different ASTContexts (even
854   // though all these different source ASTContexts just got a copy from
855   // one source AST).
856   if (origin.Valid()) {
857     auto R = m_master.CopyDecl(&getToContext(), origin.decl);
858     if (R) {
859       RegisterImportedDecl(From, R);
860       return R;
861     }
862   }
863 
864   // If we have a forcefully completed type, try to find an actual definition
865   // for it in other modules.
866   const ClangASTMetadata *md = m_master.GetDeclMetadata(From);
867   auto *td = dyn_cast<TagDecl>(From);
868   if (td && md && md->IsForcefullyCompleted()) {
869     Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
870     LLDB_LOG(log,
871              "[ClangASTImporter] Searching for a complete definition of {0} in "
872              "other modules",
873              td->getName());
874     Expected<DeclContext *> dc_or_err = ImportContext(td->getDeclContext());
875     if (!dc_or_err)
876       return dc_or_err.takeError();
877     Expected<DeclarationName> dn_or_err = Import(td->getDeclName());
878     if (!dn_or_err)
879       return dn_or_err.takeError();
880     DeclContext *dc = *dc_or_err;
881     DeclContext::lookup_result lr = dc->lookup(*dn_or_err);
882     for (clang::Decl *candidate : lr) {
883       if (candidate->getKind() == From->getKind()) {
884         RegisterImportedDecl(From, candidate);
885         m_decls_to_ignore.insert(candidate);
886         return candidate;
887       }
888     }
889     LLDB_LOG(log, "[ClangASTImporter] Complete definition not found");
890   }
891 
892   return ASTImporter::ImportImpl(From);
893 }
894 
ImportDefinitionTo(clang::Decl * to,clang::Decl * from)895 void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(
896     clang::Decl *to, clang::Decl *from) {
897   // We might have a forward declaration from a shared library that we
898   // gave external lexical storage so that Clang asks us about the full
899   // definition when it needs it. In this case the ASTImporter isn't aware
900   // that the forward decl from the shared library is the actual import
901   // target but would create a second declaration that would then be defined.
902   // We want that 'to' is actually complete after this function so let's
903   // tell the ASTImporter that 'to' was imported from 'from'.
904   MapImported(from, to);
905   ASTImporter::Imported(from, to);
906 
907   /*
908   if (to_objc_interface)
909       to_objc_interface->startDefinition();
910 
911   CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
912 
913   if (to_cxx_record)
914       to_cxx_record->startDefinition();
915   */
916 
917   Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
918 
919   if (llvm::Error err = ImportDefinition(from)) {
920     LLDB_LOG_ERROR(log, std::move(err),
921                    "[ClangASTImporter] Error during importing definition: {0}");
922     return;
923   }
924 
925   if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
926     if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
927       to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
928 
929       if (Log *log_ast =
930               lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_AST)) {
931         std::string name_string;
932         if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
933           llvm::raw_string_ostream name_stream(name_string);
934           from_named_decl->printName(name_stream);
935           name_stream.flush();
936         }
937         LLDB_LOG(log_ast, "==== [ClangASTImporter][TUDecl: {0}] Imported "
938                           "({1}Decl*){2}, named {3} (from "
939                           "(Decl*){4})",
940                  static_cast<void *>(to->getTranslationUnitDecl()),
941                  from->getDeclKindName(), static_cast<void *>(to), name_string,
942                  static_cast<void *>(from));
943 
944         // Log the AST of the TU.
945         std::string ast_string;
946         llvm::raw_string_ostream ast_stream(ast_string);
947         to->getTranslationUnitDecl()->dump(ast_stream);
948         LLDB_LOG(log_ast, "{0}", ast_string);
949       }
950     }
951   }
952 
953   // If we're dealing with an Objective-C class, ensure that the inheritance
954   // has been set up correctly.  The ASTImporter may not do this correctly if
955   // the class was originally sourced from symbols.
956 
957   if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
958     do {
959       ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
960 
961       if (to_superclass)
962         break; // we're not going to override it if it's set
963 
964       ObjCInterfaceDecl *from_objc_interface =
965           dyn_cast<ObjCInterfaceDecl>(from);
966 
967       if (!from_objc_interface)
968         break;
969 
970       ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
971 
972       if (!from_superclass)
973         break;
974 
975       llvm::Expected<Decl *> imported_from_superclass_decl =
976           Import(from_superclass);
977 
978       if (!imported_from_superclass_decl) {
979         LLDB_LOG_ERROR(log, imported_from_superclass_decl.takeError(),
980                        "Couldn't import decl: {0}");
981         break;
982       }
983 
984       ObjCInterfaceDecl *imported_from_superclass =
985           dyn_cast<ObjCInterfaceDecl>(*imported_from_superclass_decl);
986 
987       if (!imported_from_superclass)
988         break;
989 
990       if (!to_objc_interface->hasDefinition())
991         to_objc_interface->startDefinition();
992 
993       to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo(
994           m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
995     } while (false);
996   }
997 }
998 
999 /// Takes a CXXMethodDecl and completes the return type if necessary. This
1000 /// is currently only necessary for virtual functions with covariant return
1001 /// types where Clang's CodeGen expects that the underlying records are already
1002 /// completed.
MaybeCompleteReturnType(ClangASTImporter & importer,CXXMethodDecl * to_method)1003 static void MaybeCompleteReturnType(ClangASTImporter &importer,
1004                                         CXXMethodDecl *to_method) {
1005   if (!to_method->isVirtual())
1006     return;
1007   QualType return_type = to_method->getReturnType();
1008   if (!return_type->isPointerType() && !return_type->isReferenceType())
1009     return;
1010 
1011   clang::RecordDecl *rd = return_type->getPointeeType()->getAsRecordDecl();
1012   if (!rd)
1013     return;
1014   if (rd->getDefinition())
1015     return;
1016 
1017   importer.CompleteTagDecl(rd);
1018 }
1019 
1020 /// Recreate a module with its parents in \p to_source and return its id.
1021 static OptionalClangModuleID
RemapModule(OptionalClangModuleID from_id,ClangExternalASTSourceCallbacks & from_source,ClangExternalASTSourceCallbacks & to_source)1022 RemapModule(OptionalClangModuleID from_id,
1023             ClangExternalASTSourceCallbacks &from_source,
1024             ClangExternalASTSourceCallbacks &to_source) {
1025   if (!from_id.HasValue())
1026     return {};
1027   clang::Module *module = from_source.getModule(from_id.GetValue());
1028   OptionalClangModuleID parent = RemapModule(
1029       from_source.GetIDForModule(module->Parent), from_source, to_source);
1030   TypeSystemClang &to_ts = to_source.GetTypeSystem();
1031   return to_ts.GetOrCreateClangModule(module->Name, parent, module->IsFramework,
1032                                       module->IsExplicit);
1033 }
1034 
Imported(clang::Decl * from,clang::Decl * to)1035 void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
1036                                                      clang::Decl *to) {
1037   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1038 
1039   // Some decls shouldn't be tracked here because they were not created by
1040   // copying 'from' to 'to'. Just exit early for those.
1041   if (m_decls_to_ignore.count(to))
1042     return clang::ASTImporter::Imported(from, to);
1043 
1044   // Transfer module ownership information.
1045   auto *from_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1046       getFromContext().getExternalSource());
1047   // Can also be a ClangASTSourceProxy.
1048   auto *to_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1049       getToContext().getExternalSource());
1050   if (from_source && to_source) {
1051     OptionalClangModuleID from_id(from->getOwningModuleID());
1052     OptionalClangModuleID to_id =
1053         RemapModule(from_id, *from_source, *to_source);
1054     TypeSystemClang &to_ts = to_source->GetTypeSystem();
1055     to_ts.SetOwningModule(to, to_id);
1056   }
1057 
1058   lldb::user_id_t user_id = LLDB_INVALID_UID;
1059   ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
1060   if (metadata)
1061     user_id = metadata->GetUserID();
1062 
1063   if (log) {
1064     if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
1065       std::string name_string;
1066       llvm::raw_string_ostream name_stream(name_string);
1067       from_named_decl->printName(name_stream);
1068       name_stream.flush();
1069 
1070       LLDB_LOG(log,
1071                "    [ClangASTImporter] Imported ({0}Decl*){1}, named {2} (from "
1072                "(Decl*){3}), metadata {4}",
1073                from->getDeclKindName(), to, name_string, from, user_id);
1074     } else {
1075       LLDB_LOG(log,
1076                "    [ClangASTImporter] Imported ({0}Decl*){1} (from "
1077                "(Decl*){2}), metadata {3}",
1078                from->getDeclKindName(), to, from, user_id);
1079     }
1080   }
1081 
1082   ASTContextMetadataSP to_context_md =
1083       m_master.GetContextMetadata(&to->getASTContext());
1084   ASTContextMetadataSP from_context_md =
1085       m_master.MaybeGetContextMetadata(m_source_ctx);
1086 
1087   if (from_context_md) {
1088     DeclOrigin origin = from_context_md->getOrigin(from);
1089 
1090     if (origin.Valid()) {
1091       if (!to_context_md->hasOrigin(to) || user_id != LLDB_INVALID_UID)
1092         if (origin.ctx != &to->getASTContext())
1093           to_context_md->setOrigin(to, origin);
1094 
1095       ImporterDelegateSP direct_completer =
1096           m_master.GetDelegate(&to->getASTContext(), origin.ctx);
1097 
1098       if (direct_completer.get() != this)
1099         direct_completer->ASTImporter::Imported(origin.decl, to);
1100 
1101       LLDB_LOG(log,
1102                "    [ClangASTImporter] Propagated origin "
1103                "(Decl*){0}/(ASTContext*){1} from (ASTContext*){2} to "
1104                "(ASTContext*){3}",
1105                origin.decl, origin.ctx, &from->getASTContext(),
1106                &to->getASTContext());
1107     } else {
1108       if (m_new_decl_listener)
1109         m_new_decl_listener->NewDeclImported(from, to);
1110 
1111       if (!to_context_md->hasOrigin(to) || user_id != LLDB_INVALID_UID)
1112         to_context_md->setOrigin(to, DeclOrigin(m_source_ctx, from));
1113 
1114       LLDB_LOG(log,
1115                "    [ClangASTImporter] Decl has no origin information in "
1116                "(ASTContext*){0}",
1117                &from->getASTContext());
1118     }
1119 
1120     if (auto *to_namespace = dyn_cast<clang::NamespaceDecl>(to)) {
1121       auto *from_namespace = cast<clang::NamespaceDecl>(from);
1122 
1123       NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
1124 
1125       NamespaceMetaMap::iterator namespace_map_iter =
1126           namespace_maps.find(from_namespace);
1127 
1128       if (namespace_map_iter != namespace_maps.end())
1129         to_context_md->m_namespace_maps[to_namespace] =
1130             namespace_map_iter->second;
1131     }
1132   } else {
1133     to_context_md->setOrigin(to, DeclOrigin(m_source_ctx, from));
1134 
1135     LLDB_LOG(log,
1136              "    [ClangASTImporter] Sourced origin "
1137              "(Decl*){0}/(ASTContext*){1} into (ASTContext*){2}",
1138              from, m_source_ctx, &to->getASTContext());
1139   }
1140 
1141   if (auto *to_tag_decl = dyn_cast<TagDecl>(to)) {
1142     to_tag_decl->setHasExternalLexicalStorage();
1143     to_tag_decl->getPrimaryContext()->setMustBuildLookupTable();
1144     auto from_tag_decl = cast<TagDecl>(from);
1145 
1146     LLDB_LOG(
1147         log,
1148         "    [ClangASTImporter] To is a TagDecl - attributes {0}{1} [{2}->{3}]",
1149         (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1150         (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1151         (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
1152         (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
1153   }
1154 
1155   if (auto *to_namespace_decl = dyn_cast<NamespaceDecl>(to)) {
1156     m_master.BuildNamespaceMap(to_namespace_decl);
1157     to_namespace_decl->setHasExternalVisibleStorage();
1158   }
1159 
1160   if (auto *to_container_decl = dyn_cast<ObjCContainerDecl>(to)) {
1161     to_container_decl->setHasExternalLexicalStorage();
1162     to_container_decl->setHasExternalVisibleStorage();
1163 
1164     if (log) {
1165       if (ObjCInterfaceDecl *to_interface_decl =
1166               llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1167         LLDB_LOG(
1168             log,
1169             "    [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1170             "{0}{1}{2}",
1171             (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1172             (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1173             (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
1174       } else {
1175         LLDB_LOG(
1176             log, "    [ClangASTImporter] To is an {0}Decl - attributes {1}{2}",
1177             ((Decl *)to_container_decl)->getDeclKindName(),
1178             (to_container_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1179             (to_container_decl->hasExternalVisibleStorage() ? " Visible" : ""));
1180       }
1181     }
1182   }
1183 
1184   if (clang::CXXMethodDecl *to_method = dyn_cast<CXXMethodDecl>(to))
1185     MaybeCompleteReturnType(m_master, to_method);
1186 }
1187 
1188 clang::Decl *
GetOriginalDecl(clang::Decl * To)1189 ClangASTImporter::ASTImporterDelegate::GetOriginalDecl(clang::Decl *To) {
1190   return m_master.GetDeclOrigin(To).decl;
1191 }
1192