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 llvm::SmallVector<NamedDecl *, 32> m_decls_to_complete;
220 llvm::SmallPtrSet<NamedDecl *, 32> m_decls_already_completed;
221 clang::ASTContext *m_dst_ctx;
222 clang::ASTContext *m_src_ctx;
223 ClangASTImporter &importer;
224
225 public:
226 /// Constructs a CompleteTagDeclsScope.
227 /// \param importer The ClangASTImporter that we should observe.
228 /// \param dst_ctx The ASTContext to which Decls are imported.
229 /// \param src_ctx The ASTContext from which Decls are imported.
CompleteTagDeclsScope(ClangASTImporter & importer,clang::ASTContext * dst_ctx,clang::ASTContext * src_ctx)230 explicit CompleteTagDeclsScope(ClangASTImporter &importer,
231 clang::ASTContext *dst_ctx,
232 clang::ASTContext *src_ctx)
233 : m_delegate(importer.GetDelegate(dst_ctx, src_ctx)), m_dst_ctx(dst_ctx),
234 m_src_ctx(src_ctx), importer(importer) {
235 m_delegate->SetImportListener(this);
236 }
237
~CompleteTagDeclsScope()238 virtual ~CompleteTagDeclsScope() {
239 ClangASTImporter::ASTContextMetadataSP to_context_md =
240 importer.GetContextMetadata(m_dst_ctx);
241
242 // Complete all decls we collected until now.
243 while (!m_decls_to_complete.empty()) {
244 NamedDecl *decl = m_decls_to_complete.pop_back_val();
245 m_decls_already_completed.insert(decl);
246
247 // We should only complete decls coming from the source context.
248 assert(to_context_md->m_origins[decl].ctx == m_src_ctx);
249
250 Decl *original_decl = to_context_md->m_origins[decl].decl;
251
252 // Complete the decl now.
253 TypeSystemClang::GetCompleteDecl(m_src_ctx, original_decl);
254 if (auto *tag_decl = dyn_cast<TagDecl>(decl)) {
255 if (auto *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
256 if (original_tag_decl->isCompleteDefinition()) {
257 m_delegate->ImportDefinitionTo(tag_decl, original_tag_decl);
258 tag_decl->setCompleteDefinition(true);
259 }
260 }
261
262 tag_decl->setHasExternalLexicalStorage(false);
263 tag_decl->setHasExternalVisibleStorage(false);
264 } else if (auto *container_decl = dyn_cast<ObjCContainerDecl>(decl)) {
265 container_decl->setHasExternalLexicalStorage(false);
266 container_decl->setHasExternalVisibleStorage(false);
267 }
268
269 to_context_md->m_origins.erase(decl);
270 }
271
272 // Stop listening to imported decls. We do this after clearing the
273 // Decls we needed to import to catch all Decls they might have pulled in.
274 m_delegate->RemoveImportListener();
275 }
276
NewDeclImported(clang::Decl * from,clang::Decl * to)277 void NewDeclImported(clang::Decl *from, clang::Decl *to) override {
278 // Filter out decls that we can't complete later.
279 if (!isa<TagDecl>(to) && !isa<ObjCInterfaceDecl>(to))
280 return;
281 RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
282 // We don't need to complete injected class name decls.
283 if (from_record_decl && from_record_decl->isInjectedClassName())
284 return;
285
286 NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
287 // Check if we already completed this type.
288 if (m_decls_already_completed.count(to_named_decl) != 0)
289 return;
290 m_decls_to_complete.push_back(to_named_decl);
291 }
292 };
293 } // namespace
294
DeportType(TypeSystemClang & dst,const CompilerType & src_type)295 CompilerType ClangASTImporter::DeportType(TypeSystemClang &dst,
296 const CompilerType &src_type) {
297 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
298
299 TypeSystemClang *src_ctxt =
300 llvm::cast<TypeSystemClang>(src_type.GetTypeSystem());
301
302 LLDB_LOG(log,
303 " [ClangASTImporter] DeportType called on ({0}Type*){1} "
304 "from (ASTContext*){2} to (ASTContext*){3}",
305 src_type.GetTypeName(), src_type.GetOpaqueQualType(),
306 &src_ctxt->getASTContext(), &dst.getASTContext());
307
308 DeclContextOverride decl_context_override;
309
310 if (auto *t = ClangUtil::GetQualType(src_type)->getAs<TagType>())
311 decl_context_override.OverrideAllDeclsFromContainingFunction(t->getDecl());
312
313 CompleteTagDeclsScope complete_scope(*this, &dst.getASTContext(),
314 &src_ctxt->getASTContext());
315 return CopyType(dst, src_type);
316 }
317
DeportDecl(clang::ASTContext * dst_ctx,clang::Decl * decl)318 clang::Decl *ClangASTImporter::DeportDecl(clang::ASTContext *dst_ctx,
319 clang::Decl *decl) {
320 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
321
322 clang::ASTContext *src_ctx = &decl->getASTContext();
323 LLDB_LOG(log,
324 " [ClangASTImporter] DeportDecl called on ({0}Decl*){1} from "
325 "(ASTContext*){2} to (ASTContext*){3}",
326 decl->getDeclKindName(), decl, src_ctx, dst_ctx);
327
328 DeclContextOverride decl_context_override;
329
330 decl_context_override.OverrideAllDeclsFromContainingFunction(decl);
331
332 clang::Decl *result;
333 {
334 CompleteTagDeclsScope complete_scope(*this, dst_ctx, src_ctx);
335 result = CopyDecl(dst_ctx, decl);
336 }
337
338 if (!result)
339 return nullptr;
340
341 LLDB_LOG(log,
342 " [ClangASTImporter] DeportDecl deported ({0}Decl*){1} to "
343 "({2}Decl*){3}",
344 decl->getDeclKindName(), decl, result->getDeclKindName(), result);
345
346 return result;
347 }
348
CanImport(const CompilerType & type)349 bool ClangASTImporter::CanImport(const CompilerType &type) {
350 if (!ClangUtil::IsClangType(type))
351 return false;
352
353 // TODO: remove external completion BOOL
354 // CompleteAndFetchChildren should get the Decl out and check for the
355
356 clang::QualType qual_type(
357 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
358
359 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
360 switch (type_class) {
361 case clang::Type::Record: {
362 const clang::CXXRecordDecl *cxx_record_decl =
363 qual_type->getAsCXXRecordDecl();
364 if (cxx_record_decl) {
365 if (GetDeclOrigin(cxx_record_decl).Valid())
366 return true;
367 }
368 } break;
369
370 case clang::Type::Enum: {
371 clang::EnumDecl *enum_decl =
372 llvm::cast<clang::EnumType>(qual_type)->getDecl();
373 if (enum_decl) {
374 if (GetDeclOrigin(enum_decl).Valid())
375 return true;
376 }
377 } break;
378
379 case clang::Type::ObjCObject:
380 case clang::Type::ObjCInterface: {
381 const clang::ObjCObjectType *objc_class_type =
382 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
383 if (objc_class_type) {
384 clang::ObjCInterfaceDecl *class_interface_decl =
385 objc_class_type->getInterface();
386 // We currently can't complete objective C types through the newly added
387 // ASTContext because it only supports TagDecl objects right now...
388 if (class_interface_decl) {
389 if (GetDeclOrigin(class_interface_decl).Valid())
390 return true;
391 }
392 }
393 } break;
394
395 case clang::Type::Typedef:
396 return CanImport(CompilerType(type.GetTypeSystem(),
397 llvm::cast<clang::TypedefType>(qual_type)
398 ->getDecl()
399 ->getUnderlyingType()
400 .getAsOpaquePtr()));
401
402 case clang::Type::Auto:
403 return CanImport(CompilerType(type.GetTypeSystem(),
404 llvm::cast<clang::AutoType>(qual_type)
405 ->getDeducedType()
406 .getAsOpaquePtr()));
407
408 case clang::Type::Elaborated:
409 return CanImport(CompilerType(type.GetTypeSystem(),
410 llvm::cast<clang::ElaboratedType>(qual_type)
411 ->getNamedType()
412 .getAsOpaquePtr()));
413
414 case clang::Type::Paren:
415 return CanImport(CompilerType(
416 type.GetTypeSystem(),
417 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
418
419 default:
420 break;
421 }
422
423 return false;
424 }
425
Import(const CompilerType & type)426 bool ClangASTImporter::Import(const CompilerType &type) {
427 if (!ClangUtil::IsClangType(type))
428 return false;
429 // TODO: remove external completion BOOL
430 // CompleteAndFetchChildren should get the Decl out and check for the
431
432 clang::QualType qual_type(
433 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
434
435 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
436 switch (type_class) {
437 case clang::Type::Record: {
438 const clang::CXXRecordDecl *cxx_record_decl =
439 qual_type->getAsCXXRecordDecl();
440 if (cxx_record_decl) {
441 if (GetDeclOrigin(cxx_record_decl).Valid())
442 return CompleteAndFetchChildren(qual_type);
443 }
444 } break;
445
446 case clang::Type::Enum: {
447 clang::EnumDecl *enum_decl =
448 llvm::cast<clang::EnumType>(qual_type)->getDecl();
449 if (enum_decl) {
450 if (GetDeclOrigin(enum_decl).Valid())
451 return CompleteAndFetchChildren(qual_type);
452 }
453 } break;
454
455 case clang::Type::ObjCObject:
456 case clang::Type::ObjCInterface: {
457 const clang::ObjCObjectType *objc_class_type =
458 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
459 if (objc_class_type) {
460 clang::ObjCInterfaceDecl *class_interface_decl =
461 objc_class_type->getInterface();
462 // We currently can't complete objective C types through the newly added
463 // ASTContext because it only supports TagDecl objects right now...
464 if (class_interface_decl) {
465 if (GetDeclOrigin(class_interface_decl).Valid())
466 return CompleteAndFetchChildren(qual_type);
467 }
468 }
469 } break;
470
471 case clang::Type::Typedef:
472 return Import(CompilerType(type.GetTypeSystem(),
473 llvm::cast<clang::TypedefType>(qual_type)
474 ->getDecl()
475 ->getUnderlyingType()
476 .getAsOpaquePtr()));
477
478 case clang::Type::Auto:
479 return Import(CompilerType(type.GetTypeSystem(),
480 llvm::cast<clang::AutoType>(qual_type)
481 ->getDeducedType()
482 .getAsOpaquePtr()));
483
484 case clang::Type::Elaborated:
485 return Import(CompilerType(type.GetTypeSystem(),
486 llvm::cast<clang::ElaboratedType>(qual_type)
487 ->getNamedType()
488 .getAsOpaquePtr()));
489
490 case clang::Type::Paren:
491 return Import(CompilerType(
492 type.GetTypeSystem(),
493 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
494
495 default:
496 break;
497 }
498 return false;
499 }
500
CompleteType(const CompilerType & compiler_type)501 bool ClangASTImporter::CompleteType(const CompilerType &compiler_type) {
502 if (!CanImport(compiler_type))
503 return false;
504
505 if (Import(compiler_type)) {
506 TypeSystemClang::CompleteTagDeclarationDefinition(compiler_type);
507 return true;
508 }
509
510 TypeSystemClang::SetHasExternalStorage(compiler_type.GetOpaqueQualType(),
511 false);
512 return false;
513 }
514
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)515 bool ClangASTImporter::LayoutRecordType(
516 const clang::RecordDecl *record_decl, uint64_t &bit_size,
517 uint64_t &alignment,
518 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
519 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
520 &base_offsets,
521 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
522 &vbase_offsets) {
523 RecordDeclToLayoutMap::iterator pos =
524 m_record_decl_to_layout_map.find(record_decl);
525 bool success = false;
526 base_offsets.clear();
527 vbase_offsets.clear();
528 if (pos != m_record_decl_to_layout_map.end()) {
529 bit_size = pos->second.bit_size;
530 alignment = pos->second.alignment;
531 field_offsets.swap(pos->second.field_offsets);
532 base_offsets.swap(pos->second.base_offsets);
533 vbase_offsets.swap(pos->second.vbase_offsets);
534 m_record_decl_to_layout_map.erase(pos);
535 success = true;
536 } else {
537 bit_size = 0;
538 alignment = 0;
539 field_offsets.clear();
540 }
541 return success;
542 }
543
SetRecordLayout(clang::RecordDecl * decl,const LayoutInfo & layout)544 void ClangASTImporter::SetRecordLayout(clang::RecordDecl *decl,
545 const LayoutInfo &layout) {
546 m_record_decl_to_layout_map.insert(std::make_pair(decl, layout));
547 }
548
CompleteTagDecl(clang::TagDecl * decl)549 bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
550 DeclOrigin decl_origin = GetDeclOrigin(decl);
551
552 if (!decl_origin.Valid())
553 return false;
554
555 if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
556 return false;
557
558 ImporterDelegateSP delegate_sp(
559 GetDelegate(&decl->getASTContext(), decl_origin.ctx));
560
561 ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
562 &decl->getASTContext());
563 if (delegate_sp)
564 delegate_sp->ImportDefinitionTo(decl, decl_origin.decl);
565
566 return true;
567 }
568
CompleteTagDeclWithOrigin(clang::TagDecl * decl,clang::TagDecl * origin_decl)569 bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl,
570 clang::TagDecl *origin_decl) {
571 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
572
573 if (!TypeSystemClang::GetCompleteDecl(origin_ast_ctx, origin_decl))
574 return false;
575
576 ImporterDelegateSP delegate_sp(
577 GetDelegate(&decl->getASTContext(), origin_ast_ctx));
578
579 if (delegate_sp)
580 delegate_sp->ImportDefinitionTo(decl, origin_decl);
581
582 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
583
584 OriginMap &origins = context_md->m_origins;
585
586 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
587
588 return true;
589 }
590
CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl * interface_decl)591 bool ClangASTImporter::CompleteObjCInterfaceDecl(
592 clang::ObjCInterfaceDecl *interface_decl) {
593 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
594
595 if (!decl_origin.Valid())
596 return false;
597
598 if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
599 return false;
600
601 ImporterDelegateSP delegate_sp(
602 GetDelegate(&interface_decl->getASTContext(), decl_origin.ctx));
603
604 if (delegate_sp)
605 delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
606
607 if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
608 RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
609
610 return true;
611 }
612
CompleteAndFetchChildren(clang::QualType type)613 bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
614 if (!RequireCompleteType(type))
615 return false;
616
617 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
618
619 if (const TagType *tag_type = type->getAs<TagType>()) {
620 TagDecl *tag_decl = tag_type->getDecl();
621
622 DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
623
624 if (!decl_origin.Valid())
625 return false;
626
627 ImporterDelegateSP delegate_sp(
628 GetDelegate(&tag_decl->getASTContext(), decl_origin.ctx));
629
630 ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
631 &tag_decl->getASTContext());
632
633 TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
634
635 for (Decl *origin_child_decl : origin_tag_decl->decls()) {
636 llvm::Expected<Decl *> imported_or_err =
637 delegate_sp->Import(origin_child_decl);
638 if (!imported_or_err) {
639 LLDB_LOG_ERROR(log, imported_or_err.takeError(),
640 "Couldn't import decl: {0}");
641 return false;
642 }
643 }
644
645 if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl))
646 record_decl->setHasLoadedFieldsFromExternalStorage(true);
647
648 return true;
649 }
650
651 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
652 if (ObjCInterfaceDecl *objc_interface_decl =
653 objc_object_type->getInterface()) {
654 DeclOrigin decl_origin = GetDeclOrigin(objc_interface_decl);
655
656 if (!decl_origin.Valid())
657 return false;
658
659 ImporterDelegateSP delegate_sp(
660 GetDelegate(&objc_interface_decl->getASTContext(), decl_origin.ctx));
661
662 ObjCInterfaceDecl *origin_interface_decl =
663 llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
664
665 for (Decl *origin_child_decl : origin_interface_decl->decls()) {
666 llvm::Expected<Decl *> imported_or_err =
667 delegate_sp->Import(origin_child_decl);
668 if (!imported_or_err) {
669 LLDB_LOG_ERROR(log, imported_or_err.takeError(),
670 "Couldn't import decl: {0}");
671 return false;
672 }
673 }
674
675 return true;
676 }
677 return false;
678 }
679
680 return true;
681 }
682
RequireCompleteType(clang::QualType type)683 bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
684 if (type.isNull())
685 return false;
686
687 if (const TagType *tag_type = type->getAs<TagType>()) {
688 TagDecl *tag_decl = tag_type->getDecl();
689
690 if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
691 return true;
692
693 return CompleteTagDecl(tag_decl);
694 }
695 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
696 if (ObjCInterfaceDecl *objc_interface_decl =
697 objc_object_type->getInterface())
698 return CompleteObjCInterfaceDecl(objc_interface_decl);
699 return false;
700 }
701 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
702 return RequireCompleteType(array_type->getElementType());
703 if (const AtomicType *atomic_type = type->getAs<AtomicType>())
704 return RequireCompleteType(atomic_type->getPointeeType());
705
706 return true;
707 }
708
GetDeclMetadata(const clang::Decl * decl)709 ClangASTMetadata *ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
710 DeclOrigin decl_origin = GetDeclOrigin(decl);
711
712 if (decl_origin.Valid()) {
713 TypeSystemClang *ast = TypeSystemClang::GetASTContext(decl_origin.ctx);
714 return ast->GetMetadata(decl_origin.decl);
715 }
716 TypeSystemClang *ast = TypeSystemClang::GetASTContext(&decl->getASTContext());
717 return ast->GetMetadata(decl);
718 }
719
720 ClangASTImporter::DeclOrigin
GetDeclOrigin(const clang::Decl * decl)721 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) {
722 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
723
724 OriginMap &origins = context_md->m_origins;
725
726 OriginMap::iterator iter = origins.find(decl);
727
728 if (iter != origins.end())
729 return iter->second;
730 return DeclOrigin();
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
737 OriginMap &origins = context_md->m_origins;
738
739 OriginMap::iterator iter = origins.find(decl);
740
741 if (iter != origins.end()) {
742 iter->second.decl = original_decl;
743 iter->second.ctx = &original_decl->getASTContext();
744 return;
745 }
746 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
747 }
748
RegisterNamespaceMap(const clang::NamespaceDecl * decl,NamespaceMapSP & namespace_map)749 void ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
750 NamespaceMapSP &namespace_map) {
751 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
752
753 context_md->m_namespace_maps[decl] = namespace_map;
754 }
755
756 ClangASTImporter::NamespaceMapSP
GetNamespaceMap(const clang::NamespaceDecl * decl)757 ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl) {
758 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
759
760 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
761
762 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
763
764 if (iter != namespace_maps.end())
765 return iter->second;
766 return NamespaceMapSP();
767 }
768
BuildNamespaceMap(const clang::NamespaceDecl * decl)769 void ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl) {
770 assert(decl);
771 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
772
773 const DeclContext *parent_context = decl->getDeclContext();
774 const NamespaceDecl *parent_namespace =
775 dyn_cast<NamespaceDecl>(parent_context);
776 NamespaceMapSP parent_map;
777
778 if (parent_namespace)
779 parent_map = GetNamespaceMap(parent_namespace);
780
781 NamespaceMapSP new_map;
782
783 new_map = std::make_shared<NamespaceMap>();
784
785 if (context_md->m_map_completer) {
786 std::string namespace_string = decl->getDeclName().getAsString();
787
788 context_md->m_map_completer->CompleteNamespaceMap(
789 new_map, ConstString(namespace_string.c_str()), parent_map);
790 }
791
792 context_md->m_namespace_maps[decl] = new_map;
793 }
794
ForgetDestination(clang::ASTContext * dst_ast)795 void ClangASTImporter::ForgetDestination(clang::ASTContext *dst_ast) {
796 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
797
798 LLDB_LOG(log,
799 " [ClangASTImporter] Forgetting destination (ASTContext*){0}",
800 dst_ast);
801
802 m_metadata_map.erase(dst_ast);
803 }
804
ForgetSource(clang::ASTContext * dst_ast,clang::ASTContext * src_ast)805 void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,
806 clang::ASTContext *src_ast) {
807 ASTContextMetadataSP md = MaybeGetContextMetadata(dst_ast);
808
809 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
810
811 LLDB_LOG(log,
812 " [ClangASTImporter] Forgetting source->dest "
813 "(ASTContext*){0}->(ASTContext*){1}",
814 src_ast, dst_ast);
815
816 if (!md)
817 return;
818
819 md->m_delegates.erase(src_ast);
820
821 for (OriginMap::iterator iter = md->m_origins.begin();
822 iter != md->m_origins.end();) {
823 if (iter->second.ctx == src_ast)
824 md->m_origins.erase(iter++);
825 else
826 ++iter;
827 }
828 }
829
~MapCompleter()830 ClangASTImporter::MapCompleter::~MapCompleter() { return; }
831
832 llvm::Expected<Decl *>
ImportImpl(Decl * From)833 ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {
834 if (m_std_handler) {
835 llvm::Optional<Decl *> D = m_std_handler->Import(From);
836 if (D) {
837 // Make sure we don't use this decl later to map it back to it's original
838 // decl. The decl the CxxModuleHandler created has nothing to do with
839 // the one from debug info, and linking those two would just cause the
840 // ASTImporter to try 'updating' the module decl with the minimal one from
841 // the debug info.
842 m_decls_to_ignore.insert(*D);
843 return *D;
844 }
845 }
846
847 // Check which ASTContext this declaration originally came from.
848 DeclOrigin origin = m_master.GetDeclOrigin(From);
849 // If it originally came from the target ASTContext then we can just
850 // pretend that the original is the one we imported. This can happen for
851 // example when inspecting a persistent declaration from the scratch
852 // ASTContext (which will provide the declaration when parsing the
853 // expression and then we later try to copy the declaration back to the
854 // scratch ASTContext to store the result).
855 // Without this check we would ask the ASTImporter to import a declaration
856 // into the same ASTContext where it came from (which doesn't make a lot of
857 // sense).
858 if (origin.Valid() && origin.ctx == &getToContext()) {
859 RegisterImportedDecl(From, origin.decl);
860 return origin.decl;
861 }
862
863 // This declaration came originally from another ASTContext. Instead of
864 // copying our potentially incomplete 'From' Decl we instead go to the
865 // original ASTContext and copy the original to the target. This is not
866 // only faster than first completing our current decl and then copying it
867 // to the target, but it also prevents that indirectly copying the same
868 // declaration to the same target requires the ASTImporter to merge all
869 // the different decls that appear to come from different ASTContexts (even
870 // though all these different source ASTContexts just got a copy from
871 // one source AST).
872 if (origin.Valid()) {
873 auto R = m_master.CopyDecl(&getToContext(), origin.decl);
874 if (R) {
875 RegisterImportedDecl(From, R);
876 return R;
877 }
878 }
879
880 // If we have a forcefully completed type, try to find an actual definition
881 // for it in other modules.
882 const ClangASTMetadata *md = m_master.GetDeclMetadata(From);
883 auto *td = dyn_cast<TagDecl>(From);
884 if (td && md && md->IsForcefullyCompleted()) {
885 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
886 LLDB_LOG(log,
887 "[ClangASTImporter] Searching for a complete definition of {0} in "
888 "other modules",
889 td->getName());
890 Expected<DeclContext *> dc_or_err = ImportContext(td->getDeclContext());
891 if (!dc_or_err)
892 return dc_or_err.takeError();
893 Expected<DeclarationName> dn_or_err = Import(td->getDeclName());
894 if (!dn_or_err)
895 return dn_or_err.takeError();
896 DeclContext *dc = *dc_or_err;
897 DeclContext::lookup_result lr = dc->lookup(*dn_or_err);
898 if (lr.size()) {
899 clang::Decl *lookup_found = lr.front();
900 RegisterImportedDecl(From, lookup_found);
901 m_decls_to_ignore.insert(lookup_found);
902 return lookup_found;
903 } else
904 LLDB_LOG(log, "[ClangASTImporter] Complete definition not found");
905 }
906
907 return ASTImporter::ImportImpl(From);
908 }
909
ImportDefinitionTo(clang::Decl * to,clang::Decl * from)910 void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(
911 clang::Decl *to, clang::Decl *from) {
912 // We might have a forward declaration from a shared library that we
913 // gave external lexical storage so that Clang asks us about the full
914 // definition when it needs it. In this case the ASTImporter isn't aware
915 // that the forward decl from the shared library is the actual import
916 // target but would create a second declaration that would then be defined.
917 // We want that 'to' is actually complete after this function so let's
918 // tell the ASTImporter that 'to' was imported from 'from'.
919 MapImported(from, to);
920 ASTImporter::Imported(from, to);
921
922 /*
923 if (to_objc_interface)
924 to_objc_interface->startDefinition();
925
926 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
927
928 if (to_cxx_record)
929 to_cxx_record->startDefinition();
930 */
931
932 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
933
934 if (llvm::Error err = ImportDefinition(from)) {
935 LLDB_LOG_ERROR(log, std::move(err),
936 "[ClangASTImporter] Error during importing definition: {0}");
937 return;
938 }
939
940 if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
941 if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
942 to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
943
944 if (Log *log_ast =
945 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_AST)) {
946 std::string name_string;
947 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
948 llvm::raw_string_ostream name_stream(name_string);
949 from_named_decl->printName(name_stream);
950 name_stream.flush();
951 }
952 LLDB_LOG(log_ast, "==== [ClangASTImporter][TUDecl: {0}] Imported "
953 "({1}Decl*){2}, named {3} (from "
954 "(Decl*){4})",
955 static_cast<void *>(to->getTranslationUnitDecl()),
956 from->getDeclKindName(), static_cast<void *>(to), name_string,
957 static_cast<void *>(from));
958
959 // Log the AST of the TU.
960 std::string ast_string;
961 llvm::raw_string_ostream ast_stream(ast_string);
962 to->getTranslationUnitDecl()->dump(ast_stream);
963 LLDB_LOG(log_ast, "{0}", ast_string);
964 }
965 }
966 }
967
968 // If we're dealing with an Objective-C class, ensure that the inheritance
969 // has been set up correctly. The ASTImporter may not do this correctly if
970 // the class was originally sourced from symbols.
971
972 if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
973 do {
974 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
975
976 if (to_superclass)
977 break; // we're not going to override it if it's set
978
979 ObjCInterfaceDecl *from_objc_interface =
980 dyn_cast<ObjCInterfaceDecl>(from);
981
982 if (!from_objc_interface)
983 break;
984
985 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
986
987 if (!from_superclass)
988 break;
989
990 llvm::Expected<Decl *> imported_from_superclass_decl =
991 Import(from_superclass);
992
993 if (!imported_from_superclass_decl) {
994 LLDB_LOG_ERROR(log, imported_from_superclass_decl.takeError(),
995 "Couldn't import decl: {0}");
996 break;
997 }
998
999 ObjCInterfaceDecl *imported_from_superclass =
1000 dyn_cast<ObjCInterfaceDecl>(*imported_from_superclass_decl);
1001
1002 if (!imported_from_superclass)
1003 break;
1004
1005 if (!to_objc_interface->hasDefinition())
1006 to_objc_interface->startDefinition();
1007
1008 to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo(
1009 m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
1010 } while (false);
1011 }
1012 }
1013
1014 /// Takes a CXXMethodDecl and completes the return type if necessary. This
1015 /// is currently only necessary for virtual functions with covariant return
1016 /// types where Clang's CodeGen expects that the underlying records are already
1017 /// completed.
MaybeCompleteReturnType(ClangASTImporter & importer,CXXMethodDecl * to_method)1018 static void MaybeCompleteReturnType(ClangASTImporter &importer,
1019 CXXMethodDecl *to_method) {
1020 if (!to_method->isVirtual())
1021 return;
1022 QualType return_type = to_method->getReturnType();
1023 if (!return_type->isPointerType() && !return_type->isReferenceType())
1024 return;
1025
1026 clang::RecordDecl *rd = return_type->getPointeeType()->getAsRecordDecl();
1027 if (!rd)
1028 return;
1029 if (rd->getDefinition())
1030 return;
1031
1032 importer.CompleteTagDecl(rd);
1033 }
1034
1035 /// Recreate a module with its parents in \p to_source and return its id.
1036 static OptionalClangModuleID
RemapModule(OptionalClangModuleID from_id,ClangExternalASTSourceCallbacks & from_source,ClangExternalASTSourceCallbacks & to_source)1037 RemapModule(OptionalClangModuleID from_id,
1038 ClangExternalASTSourceCallbacks &from_source,
1039 ClangExternalASTSourceCallbacks &to_source) {
1040 if (!from_id.HasValue())
1041 return {};
1042 clang::Module *module = from_source.getModule(from_id.GetValue());
1043 OptionalClangModuleID parent = RemapModule(
1044 from_source.GetIDForModule(module->Parent), from_source, to_source);
1045 TypeSystemClang &to_ts = to_source.GetTypeSystem();
1046 return to_ts.GetOrCreateClangModule(module->Name, parent, module->IsFramework,
1047 module->IsExplicit);
1048 }
1049
Imported(clang::Decl * from,clang::Decl * to)1050 void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
1051 clang::Decl *to) {
1052 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1053
1054 // Some decls shouldn't be tracked here because they were not created by
1055 // copying 'from' to 'to'. Just exit early for those.
1056 if (m_decls_to_ignore.count(to))
1057 return clang::ASTImporter::Imported(from, to);
1058
1059 // Transfer module ownership information.
1060 auto *from_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1061 getFromContext().getExternalSource());
1062 // Can also be a ClangASTSourceProxy.
1063 auto *to_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1064 getToContext().getExternalSource());
1065 if (from_source && to_source) {
1066 OptionalClangModuleID from_id(from->getOwningModuleID());
1067 OptionalClangModuleID to_id =
1068 RemapModule(from_id, *from_source, *to_source);
1069 TypeSystemClang &to_ts = to_source->GetTypeSystem();
1070 to_ts.SetOwningModule(to, to_id);
1071 }
1072
1073 lldb::user_id_t user_id = LLDB_INVALID_UID;
1074 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
1075 if (metadata)
1076 user_id = metadata->GetUserID();
1077
1078 if (log) {
1079 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
1080 std::string name_string;
1081 llvm::raw_string_ostream name_stream(name_string);
1082 from_named_decl->printName(name_stream);
1083 name_stream.flush();
1084
1085 LLDB_LOG(log,
1086 " [ClangASTImporter] Imported ({0}Decl*){1}, named {2} (from "
1087 "(Decl*){3}), metadata {4}",
1088 from->getDeclKindName(), to, name_string, from, user_id);
1089 } else {
1090 LLDB_LOG(log,
1091 " [ClangASTImporter] Imported ({0}Decl*){1} (from "
1092 "(Decl*){2}), metadata {3}",
1093 from->getDeclKindName(), to, from, user_id);
1094 }
1095 }
1096
1097 ASTContextMetadataSP to_context_md =
1098 m_master.GetContextMetadata(&to->getASTContext());
1099 ASTContextMetadataSP from_context_md =
1100 m_master.MaybeGetContextMetadata(m_source_ctx);
1101
1102 if (from_context_md) {
1103 OriginMap &origins = from_context_md->m_origins;
1104
1105 OriginMap::iterator origin_iter = origins.find(from);
1106
1107 if (origin_iter != origins.end()) {
1108 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
1109 user_id != LLDB_INVALID_UID) {
1110 if (origin_iter->second.ctx != &to->getASTContext())
1111 to_context_md->m_origins[to] = origin_iter->second;
1112 }
1113
1114 ImporterDelegateSP direct_completer =
1115 m_master.GetDelegate(&to->getASTContext(), origin_iter->second.ctx);
1116
1117 if (direct_completer.get() != this)
1118 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
1119
1120 LLDB_LOG(log,
1121 " [ClangASTImporter] Propagated origin "
1122 "(Decl*){0}/(ASTContext*){1} from (ASTContext*){2} to "
1123 "(ASTContext*){3}",
1124 origin_iter->second.decl, origin_iter->second.ctx,
1125 &from->getASTContext(), &to->getASTContext());
1126 } else {
1127 if (m_new_decl_listener)
1128 m_new_decl_listener->NewDeclImported(from, to);
1129
1130 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
1131 user_id != LLDB_INVALID_UID) {
1132 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1133 }
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->m_origins[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