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:
187 DeclContextOverride() = default;
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 clang::QualType qual_type(
363 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
364
365 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
366 switch (type_class) {
367 case clang::Type::Record: {
368 const clang::CXXRecordDecl *cxx_record_decl =
369 qual_type->getAsCXXRecordDecl();
370 if (cxx_record_decl) {
371 if (GetDeclOrigin(cxx_record_decl).Valid())
372 return true;
373 }
374 } break;
375
376 case clang::Type::Enum: {
377 clang::EnumDecl *enum_decl =
378 llvm::cast<clang::EnumType>(qual_type)->getDecl();
379 if (enum_decl) {
380 if (GetDeclOrigin(enum_decl).Valid())
381 return true;
382 }
383 } break;
384
385 case clang::Type::ObjCObject:
386 case clang::Type::ObjCInterface: {
387 const clang::ObjCObjectType *objc_class_type =
388 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
389 if (objc_class_type) {
390 clang::ObjCInterfaceDecl *class_interface_decl =
391 objc_class_type->getInterface();
392 // We currently can't complete objective C types through the newly added
393 // ASTContext because it only supports TagDecl objects right now...
394 if (class_interface_decl) {
395 if (GetDeclOrigin(class_interface_decl).Valid())
396 return true;
397 }
398 }
399 } break;
400
401 case clang::Type::Typedef:
402 return CanImport(CompilerType(type.GetTypeSystem(),
403 llvm::cast<clang::TypedefType>(qual_type)
404 ->getDecl()
405 ->getUnderlyingType()
406 .getAsOpaquePtr()));
407
408 case clang::Type::Auto:
409 return CanImport(CompilerType(type.GetTypeSystem(),
410 llvm::cast<clang::AutoType>(qual_type)
411 ->getDeducedType()
412 .getAsOpaquePtr()));
413
414 case clang::Type::Elaborated:
415 return CanImport(CompilerType(type.GetTypeSystem(),
416 llvm::cast<clang::ElaboratedType>(qual_type)
417 ->getNamedType()
418 .getAsOpaquePtr()));
419
420 case clang::Type::Paren:
421 return CanImport(CompilerType(
422 type.GetTypeSystem(),
423 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
424
425 default:
426 break;
427 }
428
429 return false;
430 }
431
Import(const CompilerType & type)432 bool ClangASTImporter::Import(const CompilerType &type) {
433 if (!ClangUtil::IsClangType(type))
434 return false;
435
436 clang::QualType qual_type(
437 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
438
439 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
440 switch (type_class) {
441 case clang::Type::Record: {
442 const clang::CXXRecordDecl *cxx_record_decl =
443 qual_type->getAsCXXRecordDecl();
444 if (cxx_record_decl) {
445 if (GetDeclOrigin(cxx_record_decl).Valid())
446 return CompleteAndFetchChildren(qual_type);
447 }
448 } break;
449
450 case clang::Type::Enum: {
451 clang::EnumDecl *enum_decl =
452 llvm::cast<clang::EnumType>(qual_type)->getDecl();
453 if (enum_decl) {
454 if (GetDeclOrigin(enum_decl).Valid())
455 return CompleteAndFetchChildren(qual_type);
456 }
457 } break;
458
459 case clang::Type::ObjCObject:
460 case clang::Type::ObjCInterface: {
461 const clang::ObjCObjectType *objc_class_type =
462 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
463 if (objc_class_type) {
464 clang::ObjCInterfaceDecl *class_interface_decl =
465 objc_class_type->getInterface();
466 // We currently can't complete objective C types through the newly added
467 // ASTContext because it only supports TagDecl objects right now...
468 if (class_interface_decl) {
469 if (GetDeclOrigin(class_interface_decl).Valid())
470 return CompleteAndFetchChildren(qual_type);
471 }
472 }
473 } break;
474
475 case clang::Type::Typedef:
476 return Import(CompilerType(type.GetTypeSystem(),
477 llvm::cast<clang::TypedefType>(qual_type)
478 ->getDecl()
479 ->getUnderlyingType()
480 .getAsOpaquePtr()));
481
482 case clang::Type::Auto:
483 return Import(CompilerType(type.GetTypeSystem(),
484 llvm::cast<clang::AutoType>(qual_type)
485 ->getDeducedType()
486 .getAsOpaquePtr()));
487
488 case clang::Type::Elaborated:
489 return Import(CompilerType(type.GetTypeSystem(),
490 llvm::cast<clang::ElaboratedType>(qual_type)
491 ->getNamedType()
492 .getAsOpaquePtr()));
493
494 case clang::Type::Paren:
495 return Import(CompilerType(
496 type.GetTypeSystem(),
497 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
498
499 default:
500 break;
501 }
502 return false;
503 }
504
CompleteType(const CompilerType & compiler_type)505 bool ClangASTImporter::CompleteType(const CompilerType &compiler_type) {
506 if (!CanImport(compiler_type))
507 return false;
508
509 if (Import(compiler_type)) {
510 TypeSystemClang::CompleteTagDeclarationDefinition(compiler_type);
511 return true;
512 }
513
514 TypeSystemClang::SetHasExternalStorage(compiler_type.GetOpaqueQualType(),
515 false);
516 return false;
517 }
518
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)519 bool ClangASTImporter::LayoutRecordType(
520 const clang::RecordDecl *record_decl, uint64_t &bit_size,
521 uint64_t &alignment,
522 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
523 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
524 &base_offsets,
525 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
526 &vbase_offsets) {
527 RecordDeclToLayoutMap::iterator pos =
528 m_record_decl_to_layout_map.find(record_decl);
529 bool success = false;
530 base_offsets.clear();
531 vbase_offsets.clear();
532 if (pos != m_record_decl_to_layout_map.end()) {
533 bit_size = pos->second.bit_size;
534 alignment = pos->second.alignment;
535 field_offsets.swap(pos->second.field_offsets);
536 base_offsets.swap(pos->second.base_offsets);
537 vbase_offsets.swap(pos->second.vbase_offsets);
538 m_record_decl_to_layout_map.erase(pos);
539 success = true;
540 } else {
541 bit_size = 0;
542 alignment = 0;
543 field_offsets.clear();
544 }
545 return success;
546 }
547
SetRecordLayout(clang::RecordDecl * decl,const LayoutInfo & layout)548 void ClangASTImporter::SetRecordLayout(clang::RecordDecl *decl,
549 const LayoutInfo &layout) {
550 m_record_decl_to_layout_map.insert(std::make_pair(decl, layout));
551 }
552
CompleteTagDecl(clang::TagDecl * decl)553 bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
554 DeclOrigin decl_origin = GetDeclOrigin(decl);
555
556 if (!decl_origin.Valid())
557 return false;
558
559 if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
560 return false;
561
562 ImporterDelegateSP delegate_sp(
563 GetDelegate(&decl->getASTContext(), decl_origin.ctx));
564
565 ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
566 &decl->getASTContext());
567 if (delegate_sp)
568 delegate_sp->ImportDefinitionTo(decl, decl_origin.decl);
569
570 return true;
571 }
572
CompleteTagDeclWithOrigin(clang::TagDecl * decl,clang::TagDecl * origin_decl)573 bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl,
574 clang::TagDecl *origin_decl) {
575 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
576
577 if (!TypeSystemClang::GetCompleteDecl(origin_ast_ctx, origin_decl))
578 return false;
579
580 ImporterDelegateSP delegate_sp(
581 GetDelegate(&decl->getASTContext(), origin_ast_ctx));
582
583 if (delegate_sp)
584 delegate_sp->ImportDefinitionTo(decl, origin_decl);
585
586 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
587
588 context_md->setOrigin(decl, DeclOrigin(origin_ast_ctx, origin_decl));
589 return true;
590 }
591
CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl * interface_decl)592 bool ClangASTImporter::CompleteObjCInterfaceDecl(
593 clang::ObjCInterfaceDecl *interface_decl) {
594 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
595
596 if (!decl_origin.Valid())
597 return false;
598
599 if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
600 return false;
601
602 ImporterDelegateSP delegate_sp(
603 GetDelegate(&interface_decl->getASTContext(), decl_origin.ctx));
604
605 if (delegate_sp)
606 delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
607
608 if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
609 RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
610
611 return true;
612 }
613
CompleteAndFetchChildren(clang::QualType type)614 bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
615 if (!RequireCompleteType(type))
616 return false;
617
618 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
619
620 if (const TagType *tag_type = type->getAs<TagType>()) {
621 TagDecl *tag_decl = tag_type->getDecl();
622
623 DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
624
625 if (!decl_origin.Valid())
626 return false;
627
628 ImporterDelegateSP delegate_sp(
629 GetDelegate(&tag_decl->getASTContext(), decl_origin.ctx));
630
631 ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
632 &tag_decl->getASTContext());
633
634 TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
635
636 for (Decl *origin_child_decl : origin_tag_decl->decls()) {
637 llvm::Expected<Decl *> imported_or_err =
638 delegate_sp->Import(origin_child_decl);
639 if (!imported_or_err) {
640 LLDB_LOG_ERROR(log, imported_or_err.takeError(),
641 "Couldn't import decl: {0}");
642 return false;
643 }
644 }
645
646 if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl))
647 record_decl->setHasLoadedFieldsFromExternalStorage(true);
648
649 return true;
650 }
651
652 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
653 if (ObjCInterfaceDecl *objc_interface_decl =
654 objc_object_type->getInterface()) {
655 DeclOrigin decl_origin = GetDeclOrigin(objc_interface_decl);
656
657 if (!decl_origin.Valid())
658 return false;
659
660 ImporterDelegateSP delegate_sp(
661 GetDelegate(&objc_interface_decl->getASTContext(), decl_origin.ctx));
662
663 ObjCInterfaceDecl *origin_interface_decl =
664 llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
665
666 for (Decl *origin_child_decl : origin_interface_decl->decls()) {
667 llvm::Expected<Decl *> imported_or_err =
668 delegate_sp->Import(origin_child_decl);
669 if (!imported_or_err) {
670 LLDB_LOG_ERROR(log, imported_or_err.takeError(),
671 "Couldn't import decl: {0}");
672 return false;
673 }
674 }
675
676 return true;
677 }
678 return false;
679 }
680
681 return true;
682 }
683
RequireCompleteType(clang::QualType type)684 bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
685 if (type.isNull())
686 return false;
687
688 if (const TagType *tag_type = type->getAs<TagType>()) {
689 TagDecl *tag_decl = tag_type->getDecl();
690
691 if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
692 return true;
693
694 return CompleteTagDecl(tag_decl);
695 }
696 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
697 if (ObjCInterfaceDecl *objc_interface_decl =
698 objc_object_type->getInterface())
699 return CompleteObjCInterfaceDecl(objc_interface_decl);
700 return false;
701 }
702 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
703 return RequireCompleteType(array_type->getElementType());
704 if (const AtomicType *atomic_type = type->getAs<AtomicType>())
705 return RequireCompleteType(atomic_type->getPointeeType());
706
707 return true;
708 }
709
GetDeclMetadata(const clang::Decl * decl)710 ClangASTMetadata *ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
711 DeclOrigin decl_origin = GetDeclOrigin(decl);
712
713 if (decl_origin.Valid()) {
714 TypeSystemClang *ast = TypeSystemClang::GetASTContext(decl_origin.ctx);
715 return ast->GetMetadata(decl_origin.decl);
716 }
717 TypeSystemClang *ast = TypeSystemClang::GetASTContext(&decl->getASTContext());
718 return ast->GetMetadata(decl);
719 }
720
721 ClangASTImporter::DeclOrigin
GetDeclOrigin(const clang::Decl * decl)722 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) {
723 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
724
725 return context_md->getOrigin(decl);
726 }
727
SetDeclOrigin(const clang::Decl * decl,clang::Decl * original_decl)728 void ClangASTImporter::SetDeclOrigin(const clang::Decl *decl,
729 clang::Decl *original_decl) {
730 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
731 context_md->setOrigin(
732 decl, DeclOrigin(&original_decl->getASTContext(), original_decl));
733 }
734
RegisterNamespaceMap(const clang::NamespaceDecl * decl,NamespaceMapSP & namespace_map)735 void ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
736 NamespaceMapSP &namespace_map) {
737 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
738
739 context_md->m_namespace_maps[decl] = namespace_map;
740 }
741
742 ClangASTImporter::NamespaceMapSP
GetNamespaceMap(const clang::NamespaceDecl * decl)743 ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl) {
744 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
745
746 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
747
748 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
749
750 if (iter != namespace_maps.end())
751 return iter->second;
752 return NamespaceMapSP();
753 }
754
BuildNamespaceMap(const clang::NamespaceDecl * decl)755 void ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl) {
756 assert(decl);
757 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
758
759 const DeclContext *parent_context = decl->getDeclContext();
760 const NamespaceDecl *parent_namespace =
761 dyn_cast<NamespaceDecl>(parent_context);
762 NamespaceMapSP parent_map;
763
764 if (parent_namespace)
765 parent_map = GetNamespaceMap(parent_namespace);
766
767 NamespaceMapSP new_map;
768
769 new_map = std::make_shared<NamespaceMap>();
770
771 if (context_md->m_map_completer) {
772 std::string namespace_string = decl->getDeclName().getAsString();
773
774 context_md->m_map_completer->CompleteNamespaceMap(
775 new_map, ConstString(namespace_string.c_str()), parent_map);
776 }
777
778 context_md->m_namespace_maps[decl] = new_map;
779 }
780
ForgetDestination(clang::ASTContext * dst_ast)781 void ClangASTImporter::ForgetDestination(clang::ASTContext *dst_ast) {
782 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
783
784 LLDB_LOG(log,
785 " [ClangASTImporter] Forgetting destination (ASTContext*){0}",
786 dst_ast);
787
788 m_metadata_map.erase(dst_ast);
789 }
790
ForgetSource(clang::ASTContext * dst_ast,clang::ASTContext * src_ast)791 void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,
792 clang::ASTContext *src_ast) {
793 ASTContextMetadataSP md = MaybeGetContextMetadata(dst_ast);
794
795 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
796
797 LLDB_LOG(log,
798 " [ClangASTImporter] Forgetting source->dest "
799 "(ASTContext*){0}->(ASTContext*){1}",
800 src_ast, dst_ast);
801
802 if (!md)
803 return;
804
805 md->m_delegates.erase(src_ast);
806 md->removeOriginsWithContext(src_ast);
807 }
808
~MapCompleter()809 ClangASTImporter::MapCompleter::~MapCompleter() { return; }
810
811 llvm::Expected<Decl *>
ImportImpl(Decl * From)812 ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {
813 if (m_std_handler) {
814 llvm::Optional<Decl *> D = m_std_handler->Import(From);
815 if (D) {
816 // Make sure we don't use this decl later to map it back to it's original
817 // decl. The decl the CxxModuleHandler created has nothing to do with
818 // the one from debug info, and linking those two would just cause the
819 // ASTImporter to try 'updating' the module decl with the minimal one from
820 // the debug info.
821 m_decls_to_ignore.insert(*D);
822 return *D;
823 }
824 }
825
826 // Check which ASTContext this declaration originally came from.
827 DeclOrigin origin = m_master.GetDeclOrigin(From);
828
829 // Prevent infinite recursion when the origin tracking contains a cycle.
830 assert(origin.decl != From && "Origin points to itself?");
831
832 // If it originally came from the target ASTContext then we can just
833 // pretend that the original is the one we imported. This can happen for
834 // example when inspecting a persistent declaration from the scratch
835 // ASTContext (which will provide the declaration when parsing the
836 // expression and then we later try to copy the declaration back to the
837 // scratch ASTContext to store the result).
838 // Without this check we would ask the ASTImporter to import a declaration
839 // into the same ASTContext where it came from (which doesn't make a lot of
840 // sense).
841 if (origin.Valid() && origin.ctx == &getToContext()) {
842 RegisterImportedDecl(From, origin.decl);
843 return origin.decl;
844 }
845
846 // This declaration came originally from another ASTContext. Instead of
847 // copying our potentially incomplete 'From' Decl we instead go to the
848 // original ASTContext and copy the original to the target. This is not
849 // only faster than first completing our current decl and then copying it
850 // to the target, but it also prevents that indirectly copying the same
851 // declaration to the same target requires the ASTImporter to merge all
852 // the different decls that appear to come from different ASTContexts (even
853 // though all these different source ASTContexts just got a copy from
854 // one source AST).
855 if (origin.Valid()) {
856 auto R = m_master.CopyDecl(&getToContext(), origin.decl);
857 if (R) {
858 RegisterImportedDecl(From, R);
859 return R;
860 }
861 }
862
863 // If we have a forcefully completed type, try to find an actual definition
864 // for it in other modules.
865 const ClangASTMetadata *md = m_master.GetDeclMetadata(From);
866 auto *td = dyn_cast<TagDecl>(From);
867 if (td && md && md->IsForcefullyCompleted()) {
868 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
869 LLDB_LOG(log,
870 "[ClangASTImporter] Searching for a complete definition of {0} in "
871 "other modules",
872 td->getName());
873 Expected<DeclContext *> dc_or_err = ImportContext(td->getDeclContext());
874 if (!dc_or_err)
875 return dc_or_err.takeError();
876 Expected<DeclarationName> dn_or_err = Import(td->getDeclName());
877 if (!dn_or_err)
878 return dn_or_err.takeError();
879 DeclContext *dc = *dc_or_err;
880 DeclContext::lookup_result lr = dc->lookup(*dn_or_err);
881 for (clang::Decl *candidate : lr) {
882 if (candidate->getKind() == From->getKind()) {
883 RegisterImportedDecl(From, candidate);
884 m_decls_to_ignore.insert(candidate);
885 return candidate;
886 }
887 }
888 LLDB_LOG(log, "[ClangASTImporter] Complete definition not found");
889 }
890
891 // Disable the minimal import for fields that have record types. There is
892 // no point in minimally importing the record behind their type as Clang
893 // will anyway request their definition when the FieldDecl is added to the
894 // RecordDecl (as Clang will query the FieldDecl's type for things such
895 // as a deleted constexpr destructor).
896 // By importing the type ahead of time we avoid some corner cases where
897 // the FieldDecl's record is importing in the middle of Clang's
898 // `DeclContext::addDecl` logic.
899 if (clang::FieldDecl *fd = dyn_cast<FieldDecl>(From)) {
900 // This is only necessary because we do the 'minimal import'. Remove this
901 // once LLDB stopped using that mode.
902 assert(isMinimalImport() && "Only necessary for minimal import");
903 QualType field_type = fd->getType();
904 if (field_type->isRecordType()) {
905 // First get the underlying record and minimally import it.
906 clang::TagDecl *record_decl = field_type->getAsTagDecl();
907 llvm::Expected<Decl *> imported = Import(record_decl);
908 if (!imported)
909 return imported.takeError();
910 // Check how/if the import got redirected to a different AST. Now
911 // import the definition of what was actually imported. If there is no
912 // origin then that means the record was imported by just picking a
913 // compatible type in the target AST (in which case there is no more
914 // importing to do).
915 if (clang::Decl *origin = m_master.GetDeclOrigin(*imported).decl) {
916 if (llvm::Error def_err = ImportDefinition(record_decl))
917 return std::move(def_err);
918 }
919 }
920 }
921
922 return ASTImporter::ImportImpl(From);
923 }
924
ImportDefinitionTo(clang::Decl * to,clang::Decl * from)925 void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(
926 clang::Decl *to, clang::Decl *from) {
927 // We might have a forward declaration from a shared library that we
928 // gave external lexical storage so that Clang asks us about the full
929 // definition when it needs it. In this case the ASTImporter isn't aware
930 // that the forward decl from the shared library is the actual import
931 // target but would create a second declaration that would then be defined.
932 // We want that 'to' is actually complete after this function so let's
933 // tell the ASTImporter that 'to' was imported from 'from'.
934 MapImported(from, to);
935 ASTImporter::Imported(from, to);
936
937 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
938
939 if (llvm::Error err = ImportDefinition(from)) {
940 LLDB_LOG_ERROR(log, std::move(err),
941 "[ClangASTImporter] Error during importing definition: {0}");
942 return;
943 }
944
945 if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
946 if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
947 to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
948
949 if (Log *log_ast =
950 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_AST)) {
951 std::string name_string;
952 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
953 llvm::raw_string_ostream name_stream(name_string);
954 from_named_decl->printName(name_stream);
955 name_stream.flush();
956 }
957 LLDB_LOG(log_ast, "==== [ClangASTImporter][TUDecl: {0}] Imported "
958 "({1}Decl*){2}, named {3} (from "
959 "(Decl*){4})",
960 static_cast<void *>(to->getTranslationUnitDecl()),
961 from->getDeclKindName(), static_cast<void *>(to), name_string,
962 static_cast<void *>(from));
963
964 // Log the AST of the TU.
965 std::string ast_string;
966 llvm::raw_string_ostream ast_stream(ast_string);
967 to->getTranslationUnitDecl()->dump(ast_stream);
968 LLDB_LOG(log_ast, "{0}", ast_string);
969 }
970 }
971 }
972
973 // If we're dealing with an Objective-C class, ensure that the inheritance
974 // has been set up correctly. The ASTImporter may not do this correctly if
975 // the class was originally sourced from symbols.
976
977 if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
978 do {
979 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
980
981 if (to_superclass)
982 break; // we're not going to override it if it's set
983
984 ObjCInterfaceDecl *from_objc_interface =
985 dyn_cast<ObjCInterfaceDecl>(from);
986
987 if (!from_objc_interface)
988 break;
989
990 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
991
992 if (!from_superclass)
993 break;
994
995 llvm::Expected<Decl *> imported_from_superclass_decl =
996 Import(from_superclass);
997
998 if (!imported_from_superclass_decl) {
999 LLDB_LOG_ERROR(log, imported_from_superclass_decl.takeError(),
1000 "Couldn't import decl: {0}");
1001 break;
1002 }
1003
1004 ObjCInterfaceDecl *imported_from_superclass =
1005 dyn_cast<ObjCInterfaceDecl>(*imported_from_superclass_decl);
1006
1007 if (!imported_from_superclass)
1008 break;
1009
1010 if (!to_objc_interface->hasDefinition())
1011 to_objc_interface->startDefinition();
1012
1013 to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo(
1014 m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
1015 } while (false);
1016 }
1017 }
1018
1019 /// Takes a CXXMethodDecl and completes the return type if necessary. This
1020 /// is currently only necessary for virtual functions with covariant return
1021 /// types where Clang's CodeGen expects that the underlying records are already
1022 /// completed.
MaybeCompleteReturnType(ClangASTImporter & importer,CXXMethodDecl * to_method)1023 static void MaybeCompleteReturnType(ClangASTImporter &importer,
1024 CXXMethodDecl *to_method) {
1025 if (!to_method->isVirtual())
1026 return;
1027 QualType return_type = to_method->getReturnType();
1028 if (!return_type->isPointerType() && !return_type->isReferenceType())
1029 return;
1030
1031 clang::RecordDecl *rd = return_type->getPointeeType()->getAsRecordDecl();
1032 if (!rd)
1033 return;
1034 if (rd->getDefinition())
1035 return;
1036
1037 importer.CompleteTagDecl(rd);
1038 }
1039
1040 /// Recreate a module with its parents in \p to_source and return its id.
1041 static OptionalClangModuleID
RemapModule(OptionalClangModuleID from_id,ClangExternalASTSourceCallbacks & from_source,ClangExternalASTSourceCallbacks & to_source)1042 RemapModule(OptionalClangModuleID from_id,
1043 ClangExternalASTSourceCallbacks &from_source,
1044 ClangExternalASTSourceCallbacks &to_source) {
1045 if (!from_id.HasValue())
1046 return {};
1047 clang::Module *module = from_source.getModule(from_id.GetValue());
1048 OptionalClangModuleID parent = RemapModule(
1049 from_source.GetIDForModule(module->Parent), from_source, to_source);
1050 TypeSystemClang &to_ts = to_source.GetTypeSystem();
1051 return to_ts.GetOrCreateClangModule(module->Name, parent, module->IsFramework,
1052 module->IsExplicit);
1053 }
1054
Imported(clang::Decl * from,clang::Decl * to)1055 void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
1056 clang::Decl *to) {
1057 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1058
1059 // Some decls shouldn't be tracked here because they were not created by
1060 // copying 'from' to 'to'. Just exit early for those.
1061 if (m_decls_to_ignore.count(to))
1062 return clang::ASTImporter::Imported(from, to);
1063
1064 // Transfer module ownership information.
1065 auto *from_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1066 getFromContext().getExternalSource());
1067 // Can also be a ClangASTSourceProxy.
1068 auto *to_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1069 getToContext().getExternalSource());
1070 if (from_source && to_source) {
1071 OptionalClangModuleID from_id(from->getOwningModuleID());
1072 OptionalClangModuleID to_id =
1073 RemapModule(from_id, *from_source, *to_source);
1074 TypeSystemClang &to_ts = to_source->GetTypeSystem();
1075 to_ts.SetOwningModule(to, to_id);
1076 }
1077
1078 lldb::user_id_t user_id = LLDB_INVALID_UID;
1079 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
1080 if (metadata)
1081 user_id = metadata->GetUserID();
1082
1083 if (log) {
1084 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
1085 std::string name_string;
1086 llvm::raw_string_ostream name_stream(name_string);
1087 from_named_decl->printName(name_stream);
1088 name_stream.flush();
1089
1090 LLDB_LOG(log,
1091 " [ClangASTImporter] Imported ({0}Decl*){1}, named {2} (from "
1092 "(Decl*){3}), metadata {4}",
1093 from->getDeclKindName(), to, name_string, from, user_id);
1094 } else {
1095 LLDB_LOG(log,
1096 " [ClangASTImporter] Imported ({0}Decl*){1} (from "
1097 "(Decl*){2}), metadata {3}",
1098 from->getDeclKindName(), to, from, user_id);
1099 }
1100 }
1101
1102 ASTContextMetadataSP to_context_md =
1103 m_master.GetContextMetadata(&to->getASTContext());
1104 ASTContextMetadataSP from_context_md =
1105 m_master.MaybeGetContextMetadata(m_source_ctx);
1106
1107 if (from_context_md) {
1108 DeclOrigin origin = from_context_md->getOrigin(from);
1109
1110 if (origin.Valid()) {
1111 if (origin.ctx != &to->getASTContext()) {
1112 if (!to_context_md->hasOrigin(to) || user_id != LLDB_INVALID_UID)
1113 to_context_md->setOrigin(to, origin);
1114
1115 ImporterDelegateSP direct_completer =
1116 m_master.GetDelegate(&to->getASTContext(), origin.ctx);
1117
1118 if (direct_completer.get() != this)
1119 direct_completer->ASTImporter::Imported(origin.decl, to);
1120
1121 LLDB_LOG(log,
1122 " [ClangASTImporter] Propagated origin "
1123 "(Decl*){0}/(ASTContext*){1} from (ASTContext*){2} to "
1124 "(ASTContext*){3}",
1125 origin.decl, origin.ctx, &from->getASTContext(),
1126 &to->getASTContext());
1127 }
1128 } else {
1129 if (m_new_decl_listener)
1130 m_new_decl_listener->NewDeclImported(from, to);
1131
1132 if (!to_context_md->hasOrigin(to) || user_id != LLDB_INVALID_UID)
1133 to_context_md->setOrigin(to, DeclOrigin(m_source_ctx, from));
1134
1135 LLDB_LOG(log,
1136 " [ClangASTImporter] Decl has no origin information in "
1137 "(ASTContext*){0}",
1138 &from->getASTContext());
1139 }
1140
1141 if (auto *to_namespace = dyn_cast<clang::NamespaceDecl>(to)) {
1142 auto *from_namespace = cast<clang::NamespaceDecl>(from);
1143
1144 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
1145
1146 NamespaceMetaMap::iterator namespace_map_iter =
1147 namespace_maps.find(from_namespace);
1148
1149 if (namespace_map_iter != namespace_maps.end())
1150 to_context_md->m_namespace_maps[to_namespace] =
1151 namespace_map_iter->second;
1152 }
1153 } else {
1154 to_context_md->setOrigin(to, DeclOrigin(m_source_ctx, from));
1155
1156 LLDB_LOG(log,
1157 " [ClangASTImporter] Sourced origin "
1158 "(Decl*){0}/(ASTContext*){1} into (ASTContext*){2}",
1159 from, m_source_ctx, &to->getASTContext());
1160 }
1161
1162 if (auto *to_tag_decl = dyn_cast<TagDecl>(to)) {
1163 to_tag_decl->setHasExternalLexicalStorage();
1164 to_tag_decl->getPrimaryContext()->setMustBuildLookupTable();
1165 auto from_tag_decl = cast<TagDecl>(from);
1166
1167 LLDB_LOG(
1168 log,
1169 " [ClangASTImporter] To is a TagDecl - attributes {0}{1} [{2}->{3}]",
1170 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1171 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1172 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
1173 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
1174 }
1175
1176 if (auto *to_namespace_decl = dyn_cast<NamespaceDecl>(to)) {
1177 m_master.BuildNamespaceMap(to_namespace_decl);
1178 to_namespace_decl->setHasExternalVisibleStorage();
1179 }
1180
1181 if (auto *to_container_decl = dyn_cast<ObjCContainerDecl>(to)) {
1182 to_container_decl->setHasExternalLexicalStorage();
1183 to_container_decl->setHasExternalVisibleStorage();
1184
1185 if (log) {
1186 if (ObjCInterfaceDecl *to_interface_decl =
1187 llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1188 LLDB_LOG(
1189 log,
1190 " [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1191 "{0}{1}{2}",
1192 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1193 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1194 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
1195 } else {
1196 LLDB_LOG(
1197 log, " [ClangASTImporter] To is an {0}Decl - attributes {1}{2}",
1198 ((Decl *)to_container_decl)->getDeclKindName(),
1199 (to_container_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1200 (to_container_decl->hasExternalVisibleStorage() ? " Visible" : ""));
1201 }
1202 }
1203 }
1204
1205 if (clang::CXXMethodDecl *to_method = dyn_cast<CXXMethodDecl>(to))
1206 MaybeCompleteReturnType(m_master, to_method);
1207 }
1208
1209 clang::Decl *
GetOriginalDecl(clang::Decl * To)1210 ClangASTImporter::ASTImporterDelegate::GetOriginalDecl(clang::Decl *To) {
1211 return m_master.GetDeclOrigin(To).decl;
1212 }
1213