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/LLDBLog.h"
12 #include "lldb/Utility/Log.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/AST/DeclCXX.h"
15 #include "clang/AST/DeclObjC.h"
16 #include "clang/Sema/Lookup.h"
17 #include "clang/Sema/Sema.h"
18 #include "llvm/Support/raw_ostream.h"
19 
20 #include "Plugins/ExpressionParser/Clang/ClangASTImporter.h"
21 #include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h"
22 #include "Plugins/ExpressionParser/Clang/ClangASTSource.h"
23 #include "Plugins/ExpressionParser/Clang/ClangExternalASTSourceCallbacks.h"
24 #include "Plugins/ExpressionParser/Clang/ClangUtil.h"
25 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
26 
27 #include <memory>
28 #include <optional>
29 
30 using namespace lldb_private;
31 using namespace clang;
32 
33 CompilerType ClangASTImporter::CopyType(TypeSystemClang &dst_ast,
34                                         const CompilerType &src_type) {
35   clang::ASTContext &dst_clang_ast = dst_ast.getASTContext();
36 
37   auto src_ast = src_type.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
38   if (!src_ast)
39     return CompilerType();
40 
41   clang::ASTContext &src_clang_ast = src_ast->getASTContext();
42 
43   clang::QualType src_qual_type = ClangUtil::GetQualType(src_type);
44 
45   ImporterDelegateSP delegate_sp(GetDelegate(&dst_clang_ast, &src_clang_ast));
46   if (!delegate_sp)
47     return CompilerType();
48 
49   ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, &dst_clang_ast);
50 
51   llvm::Expected<QualType> ret_or_error = delegate_sp->Import(src_qual_type);
52   if (!ret_or_error) {
53     Log *log = GetLog(LLDBLog::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.weak_from_this(), dst_clang_type);
63   return CompilerType();
64 }
65 
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 = GetLog(LLDBLog::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 
115   void OverrideOne(clang::Decl *decl) {
116     if (m_backups.contains(decl)) {
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 
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 
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 
171   void Override(clang::Decl *decl) {
172     if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
173       Log *log = GetLog(LLDBLog::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:
187   DeclContextOverride() = default;
188 
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 
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.
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 
243   ~CompleteTagDeclsScope() override {
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 
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.contains(to_named_decl))
297       return;
298     // Queue this type to be completed.
299     m_decls_to_complete.insert(to_named_decl);
300   }
301 };
302 } // namespace
303 
304 CompilerType ClangASTImporter::DeportType(TypeSystemClang &dst,
305                                           const CompilerType &src_type) {
306   Log *log = GetLog(LLDBLog::Expressions);
307 
308   auto src_ctxt = src_type.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
309   if (!src_ctxt)
310     return {};
311 
312   LLDB_LOG(log,
313            "    [ClangASTImporter] DeportType called on ({0}Type*){1} "
314            "from (ASTContext*){2} to (ASTContext*){3}",
315            src_type.GetTypeName(), src_type.GetOpaqueQualType(),
316            &src_ctxt->getASTContext(), &dst.getASTContext());
317 
318   DeclContextOverride decl_context_override;
319 
320   if (auto *t = ClangUtil::GetQualType(src_type)->getAs<TagType>())
321     decl_context_override.OverrideAllDeclsFromContainingFunction(t->getDecl());
322 
323   CompleteTagDeclsScope complete_scope(*this, &dst.getASTContext(),
324                                        &src_ctxt->getASTContext());
325   return CopyType(dst, src_type);
326 }
327 
328 clang::Decl *ClangASTImporter::DeportDecl(clang::ASTContext *dst_ctx,
329                                           clang::Decl *decl) {
330   Log *log = GetLog(LLDBLog::Expressions);
331 
332   clang::ASTContext *src_ctx = &decl->getASTContext();
333   LLDB_LOG(log,
334            "    [ClangASTImporter] DeportDecl called on ({0}Decl*){1} from "
335            "(ASTContext*){2} to (ASTContext*){3}",
336            decl->getDeclKindName(), decl, src_ctx, dst_ctx);
337 
338   DeclContextOverride decl_context_override;
339 
340   decl_context_override.OverrideAllDeclsFromContainingFunction(decl);
341 
342   clang::Decl *result;
343   {
344     CompleteTagDeclsScope complete_scope(*this, dst_ctx, src_ctx);
345     result = CopyDecl(dst_ctx, decl);
346   }
347 
348   if (!result)
349     return nullptr;
350 
351   LLDB_LOG(log,
352            "    [ClangASTImporter] DeportDecl deported ({0}Decl*){1} to "
353            "({2}Decl*){3}",
354            decl->getDeclKindName(), decl, result->getDeclKindName(), result);
355 
356   return result;
357 }
358 
359 bool ClangASTImporter::CanImport(const CompilerType &type) {
360   if (!ClangUtil::IsClangType(type))
361     return false;
362 
363   clang::QualType qual_type(
364       ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
365 
366   const clang::Type::TypeClass type_class = qual_type->getTypeClass();
367   switch (type_class) {
368   case clang::Type::Record: {
369     const clang::CXXRecordDecl *cxx_record_decl =
370         qual_type->getAsCXXRecordDecl();
371     if (cxx_record_decl) {
372       if (GetDeclOrigin(cxx_record_decl).Valid())
373         return true;
374     }
375   } break;
376 
377   case clang::Type::Enum: {
378     clang::EnumDecl *enum_decl =
379         llvm::cast<clang::EnumType>(qual_type)->getDecl();
380     if (enum_decl) {
381       if (GetDeclOrigin(enum_decl).Valid())
382         return true;
383     }
384   } break;
385 
386   case clang::Type::ObjCObject:
387   case clang::Type::ObjCInterface: {
388     const clang::ObjCObjectType *objc_class_type =
389         llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
390     if (objc_class_type) {
391       clang::ObjCInterfaceDecl *class_interface_decl =
392           objc_class_type->getInterface();
393       // We currently can't complete objective C types through the newly added
394       // ASTContext because it only supports TagDecl objects right now...
395       if (class_interface_decl) {
396         if (GetDeclOrigin(class_interface_decl).Valid())
397           return true;
398       }
399     }
400   } break;
401 
402   case clang::Type::Typedef:
403     return CanImport(CompilerType(type.GetTypeSystem(),
404                                   llvm::cast<clang::TypedefType>(qual_type)
405                                       ->getDecl()
406                                       ->getUnderlyingType()
407                                       .getAsOpaquePtr()));
408 
409   case clang::Type::Auto:
410     return CanImport(CompilerType(type.GetTypeSystem(),
411                                   llvm::cast<clang::AutoType>(qual_type)
412                                       ->getDeducedType()
413                                       .getAsOpaquePtr()));
414 
415   case clang::Type::Elaborated:
416     return CanImport(CompilerType(type.GetTypeSystem(),
417                                   llvm::cast<clang::ElaboratedType>(qual_type)
418                                       ->getNamedType()
419                                       .getAsOpaquePtr()));
420 
421   case clang::Type::Paren:
422     return CanImport(CompilerType(
423         type.GetTypeSystem(),
424         llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
425 
426   default:
427     break;
428   }
429 
430   return false;
431 }
432 
433 bool ClangASTImporter::Import(const CompilerType &type) {
434   if (!ClangUtil::IsClangType(type))
435     return false;
436 
437   clang::QualType qual_type(
438       ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
439 
440   const clang::Type::TypeClass type_class = qual_type->getTypeClass();
441   switch (type_class) {
442   case clang::Type::Record: {
443     const clang::CXXRecordDecl *cxx_record_decl =
444         qual_type->getAsCXXRecordDecl();
445     if (cxx_record_decl) {
446       if (GetDeclOrigin(cxx_record_decl).Valid())
447         return CompleteAndFetchChildren(qual_type);
448     }
449   } break;
450 
451   case clang::Type::Enum: {
452     clang::EnumDecl *enum_decl =
453         llvm::cast<clang::EnumType>(qual_type)->getDecl();
454     if (enum_decl) {
455       if (GetDeclOrigin(enum_decl).Valid())
456         return CompleteAndFetchChildren(qual_type);
457     }
458   } break;
459 
460   case clang::Type::ObjCObject:
461   case clang::Type::ObjCInterface: {
462     const clang::ObjCObjectType *objc_class_type =
463         llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
464     if (objc_class_type) {
465       clang::ObjCInterfaceDecl *class_interface_decl =
466           objc_class_type->getInterface();
467       // We currently can't complete objective C types through the newly added
468       // ASTContext because it only supports TagDecl objects right now...
469       if (class_interface_decl) {
470         if (GetDeclOrigin(class_interface_decl).Valid())
471           return CompleteAndFetchChildren(qual_type);
472       }
473     }
474   } break;
475 
476   case clang::Type::Typedef:
477     return Import(CompilerType(type.GetTypeSystem(),
478                                llvm::cast<clang::TypedefType>(qual_type)
479                                    ->getDecl()
480                                    ->getUnderlyingType()
481                                    .getAsOpaquePtr()));
482 
483   case clang::Type::Auto:
484     return Import(CompilerType(type.GetTypeSystem(),
485                                llvm::cast<clang::AutoType>(qual_type)
486                                    ->getDeducedType()
487                                    .getAsOpaquePtr()));
488 
489   case clang::Type::Elaborated:
490     return Import(CompilerType(type.GetTypeSystem(),
491                                llvm::cast<clang::ElaboratedType>(qual_type)
492                                    ->getNamedType()
493                                    .getAsOpaquePtr()));
494 
495   case clang::Type::Paren:
496     return Import(CompilerType(
497         type.GetTypeSystem(),
498         llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
499 
500   default:
501     break;
502   }
503   return false;
504 }
505 
506 bool ClangASTImporter::CompleteType(const CompilerType &compiler_type) {
507   if (!CanImport(compiler_type))
508     return false;
509 
510   if (Import(compiler_type)) {
511     TypeSystemClang::CompleteTagDeclarationDefinition(compiler_type);
512     return true;
513   }
514 
515   TypeSystemClang::SetHasExternalStorage(compiler_type.GetOpaqueQualType(),
516                                          false);
517   return false;
518 }
519 
520 bool ClangASTImporter::LayoutRecordType(
521     const clang::RecordDecl *record_decl, uint64_t &bit_size,
522     uint64_t &alignment,
523     llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
524     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
525         &base_offsets,
526     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
527         &vbase_offsets) {
528   RecordDeclToLayoutMap::iterator pos =
529       m_record_decl_to_layout_map.find(record_decl);
530   bool success = false;
531   base_offsets.clear();
532   vbase_offsets.clear();
533   if (pos != m_record_decl_to_layout_map.end()) {
534     bit_size = pos->second.bit_size;
535     alignment = pos->second.alignment;
536     field_offsets.swap(pos->second.field_offsets);
537     base_offsets.swap(pos->second.base_offsets);
538     vbase_offsets.swap(pos->second.vbase_offsets);
539     m_record_decl_to_layout_map.erase(pos);
540     success = true;
541   } else {
542     bit_size = 0;
543     alignment = 0;
544     field_offsets.clear();
545   }
546   return success;
547 }
548 
549 void ClangASTImporter::SetRecordLayout(clang::RecordDecl *decl,
550                                         const LayoutInfo &layout) {
551   m_record_decl_to_layout_map.insert(std::make_pair(decl, layout));
552 }
553 
554 bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
555   DeclOrigin decl_origin = GetDeclOrigin(decl);
556 
557   if (!decl_origin.Valid())
558     return false;
559 
560   if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
561     return false;
562 
563   ImporterDelegateSP delegate_sp(
564       GetDelegate(&decl->getASTContext(), decl_origin.ctx));
565 
566   ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
567                                                 &decl->getASTContext());
568   if (delegate_sp)
569     delegate_sp->ImportDefinitionTo(decl, decl_origin.decl);
570 
571   return true;
572 }
573 
574 bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl,
575                                                  clang::TagDecl *origin_decl) {
576   clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
577 
578   if (!TypeSystemClang::GetCompleteDecl(origin_ast_ctx, origin_decl))
579     return false;
580 
581   ImporterDelegateSP delegate_sp(
582       GetDelegate(&decl->getASTContext(), origin_ast_ctx));
583 
584   if (delegate_sp)
585     delegate_sp->ImportDefinitionTo(decl, origin_decl);
586 
587   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
588 
589   context_md->setOrigin(decl, DeclOrigin(origin_ast_ctx, origin_decl));
590   return true;
591 }
592 
593 bool ClangASTImporter::CompleteObjCInterfaceDecl(
594     clang::ObjCInterfaceDecl *interface_decl) {
595   DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
596 
597   if (!decl_origin.Valid())
598     return false;
599 
600   if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
601     return false;
602 
603   ImporterDelegateSP delegate_sp(
604       GetDelegate(&interface_decl->getASTContext(), decl_origin.ctx));
605 
606   if (delegate_sp)
607     delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
608 
609   if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
610     RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
611 
612   return true;
613 }
614 
615 bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
616   if (!RequireCompleteType(type))
617     return false;
618 
619   Log *log = GetLog(LLDBLog::Expressions);
620 
621   if (const TagType *tag_type = type->getAs<TagType>()) {
622     TagDecl *tag_decl = tag_type->getDecl();
623 
624     DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
625 
626     if (!decl_origin.Valid())
627       return false;
628 
629     ImporterDelegateSP delegate_sp(
630         GetDelegate(&tag_decl->getASTContext(), decl_origin.ctx));
631 
632     ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
633                                                   &tag_decl->getASTContext());
634 
635     TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
636 
637     for (Decl *origin_child_decl : origin_tag_decl->decls()) {
638       llvm::Expected<Decl *> imported_or_err =
639           delegate_sp->Import(origin_child_decl);
640       if (!imported_or_err) {
641         LLDB_LOG_ERROR(log, imported_or_err.takeError(),
642                        "Couldn't import decl: {0}");
643         return false;
644       }
645     }
646 
647     if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl))
648       record_decl->setHasLoadedFieldsFromExternalStorage(true);
649 
650     return true;
651   }
652 
653   if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
654     if (ObjCInterfaceDecl *objc_interface_decl =
655             objc_object_type->getInterface()) {
656       DeclOrigin decl_origin = GetDeclOrigin(objc_interface_decl);
657 
658       if (!decl_origin.Valid())
659         return false;
660 
661       ImporterDelegateSP delegate_sp(
662           GetDelegate(&objc_interface_decl->getASTContext(), decl_origin.ctx));
663 
664       ObjCInterfaceDecl *origin_interface_decl =
665           llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
666 
667       for (Decl *origin_child_decl : origin_interface_decl->decls()) {
668         llvm::Expected<Decl *> imported_or_err =
669             delegate_sp->Import(origin_child_decl);
670         if (!imported_or_err) {
671           LLDB_LOG_ERROR(log, imported_or_err.takeError(),
672                          "Couldn't import decl: {0}");
673           return false;
674         }
675       }
676 
677       return true;
678     }
679     return false;
680   }
681 
682   return true;
683 }
684 
685 bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
686   if (type.isNull())
687     return false;
688 
689   if (const TagType *tag_type = type->getAs<TagType>()) {
690     TagDecl *tag_decl = tag_type->getDecl();
691 
692     if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
693       return true;
694 
695     return CompleteTagDecl(tag_decl);
696   }
697   if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
698     if (ObjCInterfaceDecl *objc_interface_decl =
699             objc_object_type->getInterface())
700       return CompleteObjCInterfaceDecl(objc_interface_decl);
701     return false;
702   }
703   if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
704     return RequireCompleteType(array_type->getElementType());
705   if (const AtomicType *atomic_type = type->getAs<AtomicType>())
706     return RequireCompleteType(atomic_type->getPointeeType());
707 
708   return true;
709 }
710 
711 ClangASTMetadata *ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
712   DeclOrigin decl_origin = GetDeclOrigin(decl);
713 
714   if (decl_origin.Valid()) {
715     TypeSystemClang *ast = TypeSystemClang::GetASTContext(decl_origin.ctx);
716     return ast->GetMetadata(decl_origin.decl);
717   }
718   TypeSystemClang *ast = TypeSystemClang::GetASTContext(&decl->getASTContext());
719   return ast->GetMetadata(decl);
720 }
721 
722 ClangASTImporter::DeclOrigin
723 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) {
724   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
725 
726   return context_md->getOrigin(decl);
727 }
728 
729 void ClangASTImporter::SetDeclOrigin(const clang::Decl *decl,
730                                      clang::Decl *original_decl) {
731   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
732   context_md->setOrigin(
733       decl, DeclOrigin(&original_decl->getASTContext(), original_decl));
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   return NamespaceMapSP();
754 }
755 
756 void ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl) {
757   assert(decl);
758   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
759 
760   const DeclContext *parent_context = decl->getDeclContext();
761   const NamespaceDecl *parent_namespace =
762       dyn_cast<NamespaceDecl>(parent_context);
763   NamespaceMapSP parent_map;
764 
765   if (parent_namespace)
766     parent_map = GetNamespaceMap(parent_namespace);
767 
768   NamespaceMapSP new_map;
769 
770   new_map = std::make_shared<NamespaceMap>();
771 
772   if (context_md->m_map_completer) {
773     std::string namespace_string = decl->getDeclName().getAsString();
774 
775     context_md->m_map_completer->CompleteNamespaceMap(
776         new_map, ConstString(namespace_string.c_str()), parent_map);
777   }
778 
779   context_md->m_namespace_maps[decl] = new_map;
780 }
781 
782 void ClangASTImporter::ForgetDestination(clang::ASTContext *dst_ast) {
783   Log *log = GetLog(LLDBLog::Expressions);
784 
785   LLDB_LOG(log,
786            "    [ClangASTImporter] Forgetting destination (ASTContext*){0}",
787            dst_ast);
788 
789   m_metadata_map.erase(dst_ast);
790 }
791 
792 void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,
793                                     clang::ASTContext *src_ast) {
794   ASTContextMetadataSP md = MaybeGetContextMetadata(dst_ast);
795 
796   Log *log = GetLog(LLDBLog::Expressions);
797 
798   LLDB_LOG(log,
799            "    [ClangASTImporter] Forgetting source->dest "
800            "(ASTContext*){0}->(ASTContext*){1}",
801            src_ast, dst_ast);
802 
803   if (!md)
804     return;
805 
806   md->m_delegates.erase(src_ast);
807   md->removeOriginsWithContext(src_ast);
808 }
809 
810 ClangASTImporter::MapCompleter::~MapCompleter() = default;
811 
812 llvm::Expected<Decl *>
813 ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {
814   if (m_std_handler) {
815     std::optional<Decl *> D = m_std_handler->Import(From);
816     if (D) {
817       // Make sure we don't use this decl later to map it back to it's original
818       // decl. The decl the CxxModuleHandler created has nothing to do with
819       // the one from debug info, and linking those two would just cause the
820       // ASTImporter to try 'updating' the module decl with the minimal one from
821       // the debug info.
822       m_decls_to_ignore.insert(*D);
823       return *D;
824     }
825   }
826 
827   // Check which ASTContext this declaration originally came from.
828   DeclOrigin origin = m_main.GetDeclOrigin(From);
829 
830   // Prevent infinite recursion when the origin tracking contains a cycle.
831   assert(origin.decl != From && "Origin points to itself?");
832 
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_main.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_main.GetDeclMetadata(From);
867   auto *td = dyn_cast<TagDecl>(From);
868   if (td && md && md->IsForcefullyCompleted()) {
869     Log *log = GetLog(LLDBLog::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 
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   Log *log = GetLog(LLDBLog::Expressions);
908 
909   if (llvm::Error err = ImportDefinition(from)) {
910     LLDB_LOG_ERROR(log, std::move(err),
911                    "[ClangASTImporter] Error during importing definition: {0}");
912     return;
913   }
914 
915   if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
916     if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
917       to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
918 
919       if (Log *log_ast = GetLog(LLDBLog::AST)) {
920         std::string name_string;
921         if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
922           llvm::raw_string_ostream name_stream(name_string);
923           from_named_decl->printName(name_stream);
924           name_stream.flush();
925         }
926         LLDB_LOG(log_ast, "==== [ClangASTImporter][TUDecl: {0}] Imported "
927                           "({1}Decl*){2}, named {3} (from "
928                           "(Decl*){4})",
929                  static_cast<void *>(to->getTranslationUnitDecl()),
930                  from->getDeclKindName(), static_cast<void *>(to), name_string,
931                  static_cast<void *>(from));
932 
933         // Log the AST of the TU.
934         std::string ast_string;
935         llvm::raw_string_ostream ast_stream(ast_string);
936         to->getTranslationUnitDecl()->dump(ast_stream);
937         LLDB_LOG(log_ast, "{0}", ast_string);
938       }
939     }
940   }
941 
942   // If we're dealing with an Objective-C class, ensure that the inheritance
943   // has been set up correctly.  The ASTImporter may not do this correctly if
944   // the class was originally sourced from symbols.
945 
946   if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
947     do {
948       ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
949 
950       if (to_superclass)
951         break; // we're not going to override it if it's set
952 
953       ObjCInterfaceDecl *from_objc_interface =
954           dyn_cast<ObjCInterfaceDecl>(from);
955 
956       if (!from_objc_interface)
957         break;
958 
959       ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
960 
961       if (!from_superclass)
962         break;
963 
964       llvm::Expected<Decl *> imported_from_superclass_decl =
965           Import(from_superclass);
966 
967       if (!imported_from_superclass_decl) {
968         LLDB_LOG_ERROR(log, imported_from_superclass_decl.takeError(),
969                        "Couldn't import decl: {0}");
970         break;
971       }
972 
973       ObjCInterfaceDecl *imported_from_superclass =
974           dyn_cast<ObjCInterfaceDecl>(*imported_from_superclass_decl);
975 
976       if (!imported_from_superclass)
977         break;
978 
979       if (!to_objc_interface->hasDefinition())
980         to_objc_interface->startDefinition();
981 
982       to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo(
983           m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
984     } while (false);
985   }
986 }
987 
988 /// Takes a CXXMethodDecl and completes the return type if necessary. This
989 /// is currently only necessary for virtual functions with covariant return
990 /// types where Clang's CodeGen expects that the underlying records are already
991 /// completed.
992 static void MaybeCompleteReturnType(ClangASTImporter &importer,
993                                         CXXMethodDecl *to_method) {
994   if (!to_method->isVirtual())
995     return;
996   QualType return_type = to_method->getReturnType();
997   if (!return_type->isPointerType() && !return_type->isReferenceType())
998     return;
999 
1000   clang::RecordDecl *rd = return_type->getPointeeType()->getAsRecordDecl();
1001   if (!rd)
1002     return;
1003   if (rd->getDefinition())
1004     return;
1005 
1006   importer.CompleteTagDecl(rd);
1007 }
1008 
1009 /// Recreate a module with its parents in \p to_source and return its id.
1010 static OptionalClangModuleID
1011 RemapModule(OptionalClangModuleID from_id,
1012             ClangExternalASTSourceCallbacks &from_source,
1013             ClangExternalASTSourceCallbacks &to_source) {
1014   if (!from_id.HasValue())
1015     return {};
1016   clang::Module *module = from_source.getModule(from_id.GetValue());
1017   OptionalClangModuleID parent = RemapModule(
1018       from_source.GetIDForModule(module->Parent), from_source, to_source);
1019   TypeSystemClang &to_ts = to_source.GetTypeSystem();
1020   return to_ts.GetOrCreateClangModule(module->Name, parent, module->IsFramework,
1021                                       module->IsExplicit);
1022 }
1023 
1024 void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
1025                                                      clang::Decl *to) {
1026   Log *log = GetLog(LLDBLog::Expressions);
1027 
1028   // Some decls shouldn't be tracked here because they were not created by
1029   // copying 'from' to 'to'. Just exit early for those.
1030   if (m_decls_to_ignore.count(to))
1031     return clang::ASTImporter::Imported(from, to);
1032 
1033   // Transfer module ownership information.
1034   auto *from_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1035       getFromContext().getExternalSource());
1036   // Can also be a ClangASTSourceProxy.
1037   auto *to_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1038       getToContext().getExternalSource());
1039   if (from_source && to_source) {
1040     OptionalClangModuleID from_id(from->getOwningModuleID());
1041     OptionalClangModuleID to_id =
1042         RemapModule(from_id, *from_source, *to_source);
1043     TypeSystemClang &to_ts = to_source->GetTypeSystem();
1044     to_ts.SetOwningModule(to, to_id);
1045   }
1046 
1047   lldb::user_id_t user_id = LLDB_INVALID_UID;
1048   ClangASTMetadata *metadata = m_main.GetDeclMetadata(from);
1049   if (metadata)
1050     user_id = metadata->GetUserID();
1051 
1052   if (log) {
1053     if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
1054       std::string name_string;
1055       llvm::raw_string_ostream name_stream(name_string);
1056       from_named_decl->printName(name_stream);
1057       name_stream.flush();
1058 
1059       LLDB_LOG(log,
1060                "    [ClangASTImporter] Imported ({0}Decl*){1}, named {2} (from "
1061                "(Decl*){3}), metadata {4}",
1062                from->getDeclKindName(), to, name_string, from, user_id);
1063     } else {
1064       LLDB_LOG(log,
1065                "    [ClangASTImporter] Imported ({0}Decl*){1} (from "
1066                "(Decl*){2}), metadata {3}",
1067                from->getDeclKindName(), to, from, user_id);
1068     }
1069   }
1070 
1071   ASTContextMetadataSP to_context_md =
1072       m_main.GetContextMetadata(&to->getASTContext());
1073   ASTContextMetadataSP from_context_md =
1074       m_main.MaybeGetContextMetadata(m_source_ctx);
1075 
1076   if (from_context_md) {
1077     DeclOrigin origin = from_context_md->getOrigin(from);
1078 
1079     if (origin.Valid()) {
1080       if (origin.ctx != &to->getASTContext()) {
1081         if (!to_context_md->hasOrigin(to) || user_id != LLDB_INVALID_UID)
1082           to_context_md->setOrigin(to, origin);
1083 
1084         ImporterDelegateSP direct_completer =
1085             m_main.GetDelegate(&to->getASTContext(), origin.ctx);
1086 
1087         if (direct_completer.get() != this)
1088           direct_completer->ASTImporter::Imported(origin.decl, to);
1089 
1090         LLDB_LOG(log,
1091                  "    [ClangASTImporter] Propagated origin "
1092                  "(Decl*){0}/(ASTContext*){1} from (ASTContext*){2} to "
1093                  "(ASTContext*){3}",
1094                  origin.decl, origin.ctx, &from->getASTContext(),
1095                  &to->getASTContext());
1096       }
1097     } else {
1098       if (m_new_decl_listener)
1099         m_new_decl_listener->NewDeclImported(from, to);
1100 
1101       if (!to_context_md->hasOrigin(to) || user_id != LLDB_INVALID_UID)
1102         to_context_md->setOrigin(to, DeclOrigin(m_source_ctx, from));
1103 
1104       LLDB_LOG(log,
1105                "    [ClangASTImporter] Decl has no origin information in "
1106                "(ASTContext*){0}",
1107                &from->getASTContext());
1108     }
1109 
1110     if (auto *to_namespace = dyn_cast<clang::NamespaceDecl>(to)) {
1111       auto *from_namespace = cast<clang::NamespaceDecl>(from);
1112 
1113       NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
1114 
1115       NamespaceMetaMap::iterator namespace_map_iter =
1116           namespace_maps.find(from_namespace);
1117 
1118       if (namespace_map_iter != namespace_maps.end())
1119         to_context_md->m_namespace_maps[to_namespace] =
1120             namespace_map_iter->second;
1121     }
1122   } else {
1123     to_context_md->setOrigin(to, DeclOrigin(m_source_ctx, from));
1124 
1125     LLDB_LOG(log,
1126              "    [ClangASTImporter] Sourced origin "
1127              "(Decl*){0}/(ASTContext*){1} into (ASTContext*){2}",
1128              from, m_source_ctx, &to->getASTContext());
1129   }
1130 
1131   if (auto *to_tag_decl = dyn_cast<TagDecl>(to)) {
1132     to_tag_decl->setHasExternalLexicalStorage();
1133     to_tag_decl->getPrimaryContext()->setMustBuildLookupTable();
1134     auto from_tag_decl = cast<TagDecl>(from);
1135 
1136     LLDB_LOG(
1137         log,
1138         "    [ClangASTImporter] To is a TagDecl - attributes {0}{1} [{2}->{3}]",
1139         (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1140         (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1141         (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
1142         (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
1143   }
1144 
1145   if (auto *to_namespace_decl = dyn_cast<NamespaceDecl>(to)) {
1146     m_main.BuildNamespaceMap(to_namespace_decl);
1147     to_namespace_decl->setHasExternalVisibleStorage();
1148   }
1149 
1150   if (auto *to_container_decl = dyn_cast<ObjCContainerDecl>(to)) {
1151     to_container_decl->setHasExternalLexicalStorage();
1152     to_container_decl->setHasExternalVisibleStorage();
1153 
1154     if (log) {
1155       if (ObjCInterfaceDecl *to_interface_decl =
1156               llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1157         LLDB_LOG(
1158             log,
1159             "    [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1160             "{0}{1}{2}",
1161             (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1162             (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1163             (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
1164       } else {
1165         LLDB_LOG(
1166             log, "    [ClangASTImporter] To is an {0}Decl - attributes {1}{2}",
1167             ((Decl *)to_container_decl)->getDeclKindName(),
1168             (to_container_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1169             (to_container_decl->hasExternalVisibleStorage() ? " Visible" : ""));
1170       }
1171     }
1172   }
1173 
1174   if (clang::CXXMethodDecl *to_method = dyn_cast<CXXMethodDecl>(to))
1175     MaybeCompleteReturnType(m_main, to_method);
1176 }
1177 
1178 clang::Decl *
1179 ClangASTImporter::ASTImporterDelegate::GetOriginalDecl(clang::Decl *To) {
1180   return m_main.GetDeclOrigin(To).decl;
1181 }
1182