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 29 using namespace lldb_private; 30 using namespace clang; 31 32 CompilerType ClangASTImporter::CopyType(TypeSystemClang &dst_ast, 33 const CompilerType &src_type) { 34 clang::ASTContext &dst_clang_ast = dst_ast.getASTContext(); 35 36 TypeSystemClang *src_ast = 37 llvm::dyn_cast_or_null<TypeSystemClang>(src_type.GetTypeSystem()); 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, 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.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 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 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 327 clang::Decl *ClangASTImporter::DeportDecl(clang::ASTContext *dst_ctx, 328 clang::Decl *decl) { 329 Log *log = GetLog(LLDBLog::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 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 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 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 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 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 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 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 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 614 bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) { 615 if (!RequireCompleteType(type)) 616 return false; 617 618 Log *log = GetLog(LLDBLog::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 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 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 722 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) { 723 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext()); 724 725 return context_md->getOrigin(decl); 726 } 727 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 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 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 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 781 void ClangASTImporter::ForgetDestination(clang::ASTContext *dst_ast) { 782 Log *log = GetLog(LLDBLog::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 791 void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast, 792 clang::ASTContext *src_ast) { 793 ASTContextMetadataSP md = MaybeGetContextMetadata(dst_ast); 794 795 Log *log = GetLog(LLDBLog::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 809 ClangASTImporter::MapCompleter::~MapCompleter() = default; 810 811 llvm::Expected<Decl *> 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_main.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_main.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_main.GetDeclMetadata(From); 866 auto *td = dyn_cast<TagDecl>(From); 867 if (td && md && md->IsForcefullyCompleted()) { 868 Log *log = GetLog(LLDBLog::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 return ASTImporter::ImportImpl(From); 892 } 893 894 void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo( 895 clang::Decl *to, clang::Decl *from) { 896 // We might have a forward declaration from a shared library that we 897 // gave external lexical storage so that Clang asks us about the full 898 // definition when it needs it. In this case the ASTImporter isn't aware 899 // that the forward decl from the shared library is the actual import 900 // target but would create a second declaration that would then be defined. 901 // We want that 'to' is actually complete after this function so let's 902 // tell the ASTImporter that 'to' was imported from 'from'. 903 MapImported(from, to); 904 ASTImporter::Imported(from, to); 905 906 Log *log = GetLog(LLDBLog::Expressions); 907 908 if (llvm::Error err = ImportDefinition(from)) { 909 LLDB_LOG_ERROR(log, std::move(err), 910 "[ClangASTImporter] Error during importing definition: {0}"); 911 return; 912 } 913 914 if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) { 915 if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) { 916 to_tag->setCompleteDefinition(from_tag->isCompleteDefinition()); 917 918 if (Log *log_ast = GetLog(LLDBLog::AST)) { 919 std::string name_string; 920 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) { 921 llvm::raw_string_ostream name_stream(name_string); 922 from_named_decl->printName(name_stream); 923 name_stream.flush(); 924 } 925 LLDB_LOG(log_ast, "==== [ClangASTImporter][TUDecl: {0}] Imported " 926 "({1}Decl*){2}, named {3} (from " 927 "(Decl*){4})", 928 static_cast<void *>(to->getTranslationUnitDecl()), 929 from->getDeclKindName(), static_cast<void *>(to), name_string, 930 static_cast<void *>(from)); 931 932 // Log the AST of the TU. 933 std::string ast_string; 934 llvm::raw_string_ostream ast_stream(ast_string); 935 to->getTranslationUnitDecl()->dump(ast_stream); 936 LLDB_LOG(log_ast, "{0}", ast_string); 937 } 938 } 939 } 940 941 // If we're dealing with an Objective-C class, ensure that the inheritance 942 // has been set up correctly. The ASTImporter may not do this correctly if 943 // the class was originally sourced from symbols. 944 945 if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) { 946 do { 947 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass(); 948 949 if (to_superclass) 950 break; // we're not going to override it if it's set 951 952 ObjCInterfaceDecl *from_objc_interface = 953 dyn_cast<ObjCInterfaceDecl>(from); 954 955 if (!from_objc_interface) 956 break; 957 958 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass(); 959 960 if (!from_superclass) 961 break; 962 963 llvm::Expected<Decl *> imported_from_superclass_decl = 964 Import(from_superclass); 965 966 if (!imported_from_superclass_decl) { 967 LLDB_LOG_ERROR(log, imported_from_superclass_decl.takeError(), 968 "Couldn't import decl: {0}"); 969 break; 970 } 971 972 ObjCInterfaceDecl *imported_from_superclass = 973 dyn_cast<ObjCInterfaceDecl>(*imported_from_superclass_decl); 974 975 if (!imported_from_superclass) 976 break; 977 978 if (!to_objc_interface->hasDefinition()) 979 to_objc_interface->startDefinition(); 980 981 to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo( 982 m_source_ctx->getObjCInterfaceType(imported_from_superclass))); 983 } while (false); 984 } 985 } 986 987 /// Takes a CXXMethodDecl and completes the return type if necessary. This 988 /// is currently only necessary for virtual functions with covariant return 989 /// types where Clang's CodeGen expects that the underlying records are already 990 /// completed. 991 static void MaybeCompleteReturnType(ClangASTImporter &importer, 992 CXXMethodDecl *to_method) { 993 if (!to_method->isVirtual()) 994 return; 995 QualType return_type = to_method->getReturnType(); 996 if (!return_type->isPointerType() && !return_type->isReferenceType()) 997 return; 998 999 clang::RecordDecl *rd = return_type->getPointeeType()->getAsRecordDecl(); 1000 if (!rd) 1001 return; 1002 if (rd->getDefinition()) 1003 return; 1004 1005 importer.CompleteTagDecl(rd); 1006 } 1007 1008 /// Recreate a module with its parents in \p to_source and return its id. 1009 static OptionalClangModuleID 1010 RemapModule(OptionalClangModuleID from_id, 1011 ClangExternalASTSourceCallbacks &from_source, 1012 ClangExternalASTSourceCallbacks &to_source) { 1013 if (!from_id.HasValue()) 1014 return {}; 1015 clang::Module *module = from_source.getModule(from_id.GetValue()); 1016 OptionalClangModuleID parent = RemapModule( 1017 from_source.GetIDForModule(module->Parent), from_source, to_source); 1018 TypeSystemClang &to_ts = to_source.GetTypeSystem(); 1019 return to_ts.GetOrCreateClangModule(module->Name, parent, module->IsFramework, 1020 module->IsExplicit); 1021 } 1022 1023 void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from, 1024 clang::Decl *to) { 1025 Log *log = GetLog(LLDBLog::Expressions); 1026 1027 // Some decls shouldn't be tracked here because they were not created by 1028 // copying 'from' to 'to'. Just exit early for those. 1029 if (m_decls_to_ignore.count(to)) 1030 return clang::ASTImporter::Imported(from, to); 1031 1032 // Transfer module ownership information. 1033 auto *from_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>( 1034 getFromContext().getExternalSource()); 1035 // Can also be a ClangASTSourceProxy. 1036 auto *to_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>( 1037 getToContext().getExternalSource()); 1038 if (from_source && to_source) { 1039 OptionalClangModuleID from_id(from->getOwningModuleID()); 1040 OptionalClangModuleID to_id = 1041 RemapModule(from_id, *from_source, *to_source); 1042 TypeSystemClang &to_ts = to_source->GetTypeSystem(); 1043 to_ts.SetOwningModule(to, to_id); 1044 } 1045 1046 lldb::user_id_t user_id = LLDB_INVALID_UID; 1047 ClangASTMetadata *metadata = m_main.GetDeclMetadata(from); 1048 if (metadata) 1049 user_id = metadata->GetUserID(); 1050 1051 if (log) { 1052 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) { 1053 std::string name_string; 1054 llvm::raw_string_ostream name_stream(name_string); 1055 from_named_decl->printName(name_stream); 1056 name_stream.flush(); 1057 1058 LLDB_LOG(log, 1059 " [ClangASTImporter] Imported ({0}Decl*){1}, named {2} (from " 1060 "(Decl*){3}), metadata {4}", 1061 from->getDeclKindName(), to, name_string, from, user_id); 1062 } else { 1063 LLDB_LOG(log, 1064 " [ClangASTImporter] Imported ({0}Decl*){1} (from " 1065 "(Decl*){2}), metadata {3}", 1066 from->getDeclKindName(), to, from, user_id); 1067 } 1068 } 1069 1070 ASTContextMetadataSP to_context_md = 1071 m_main.GetContextMetadata(&to->getASTContext()); 1072 ASTContextMetadataSP from_context_md = 1073 m_main.MaybeGetContextMetadata(m_source_ctx); 1074 1075 if (from_context_md) { 1076 DeclOrigin origin = from_context_md->getOrigin(from); 1077 1078 if (origin.Valid()) { 1079 if (origin.ctx != &to->getASTContext()) { 1080 if (!to_context_md->hasOrigin(to) || user_id != LLDB_INVALID_UID) 1081 to_context_md->setOrigin(to, origin); 1082 1083 ImporterDelegateSP direct_completer = 1084 m_main.GetDelegate(&to->getASTContext(), origin.ctx); 1085 1086 if (direct_completer.get() != this) 1087 direct_completer->ASTImporter::Imported(origin.decl, to); 1088 1089 LLDB_LOG(log, 1090 " [ClangASTImporter] Propagated origin " 1091 "(Decl*){0}/(ASTContext*){1} from (ASTContext*){2} to " 1092 "(ASTContext*){3}", 1093 origin.decl, origin.ctx, &from->getASTContext(), 1094 &to->getASTContext()); 1095 } 1096 } else { 1097 if (m_new_decl_listener) 1098 m_new_decl_listener->NewDeclImported(from, to); 1099 1100 if (!to_context_md->hasOrigin(to) || user_id != LLDB_INVALID_UID) 1101 to_context_md->setOrigin(to, DeclOrigin(m_source_ctx, from)); 1102 1103 LLDB_LOG(log, 1104 " [ClangASTImporter] Decl has no origin information in " 1105 "(ASTContext*){0}", 1106 &from->getASTContext()); 1107 } 1108 1109 if (auto *to_namespace = dyn_cast<clang::NamespaceDecl>(to)) { 1110 auto *from_namespace = cast<clang::NamespaceDecl>(from); 1111 1112 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps; 1113 1114 NamespaceMetaMap::iterator namespace_map_iter = 1115 namespace_maps.find(from_namespace); 1116 1117 if (namespace_map_iter != namespace_maps.end()) 1118 to_context_md->m_namespace_maps[to_namespace] = 1119 namespace_map_iter->second; 1120 } 1121 } else { 1122 to_context_md->setOrigin(to, DeclOrigin(m_source_ctx, from)); 1123 1124 LLDB_LOG(log, 1125 " [ClangASTImporter] Sourced origin " 1126 "(Decl*){0}/(ASTContext*){1} into (ASTContext*){2}", 1127 from, m_source_ctx, &to->getASTContext()); 1128 } 1129 1130 if (auto *to_tag_decl = dyn_cast<TagDecl>(to)) { 1131 to_tag_decl->setHasExternalLexicalStorage(); 1132 to_tag_decl->getPrimaryContext()->setMustBuildLookupTable(); 1133 auto from_tag_decl = cast<TagDecl>(from); 1134 1135 LLDB_LOG( 1136 log, 1137 " [ClangASTImporter] To is a TagDecl - attributes {0}{1} [{2}->{3}]", 1138 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""), 1139 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""), 1140 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"), 1141 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete")); 1142 } 1143 1144 if (auto *to_namespace_decl = dyn_cast<NamespaceDecl>(to)) { 1145 m_main.BuildNamespaceMap(to_namespace_decl); 1146 to_namespace_decl->setHasExternalVisibleStorage(); 1147 } 1148 1149 if (auto *to_container_decl = dyn_cast<ObjCContainerDecl>(to)) { 1150 to_container_decl->setHasExternalLexicalStorage(); 1151 to_container_decl->setHasExternalVisibleStorage(); 1152 1153 if (log) { 1154 if (ObjCInterfaceDecl *to_interface_decl = 1155 llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) { 1156 LLDB_LOG( 1157 log, 1158 " [ClangASTImporter] To is an ObjCInterfaceDecl - attributes " 1159 "{0}{1}{2}", 1160 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""), 1161 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""), 1162 (to_interface_decl->hasDefinition() ? " HasDefinition" : "")); 1163 } else { 1164 LLDB_LOG( 1165 log, " [ClangASTImporter] To is an {0}Decl - attributes {1}{2}", 1166 ((Decl *)to_container_decl)->getDeclKindName(), 1167 (to_container_decl->hasExternalLexicalStorage() ? " Lexical" : ""), 1168 (to_container_decl->hasExternalVisibleStorage() ? " Visible" : "")); 1169 } 1170 } 1171 } 1172 1173 if (clang::CXXMethodDecl *to_method = dyn_cast<CXXMethodDecl>(to)) 1174 MaybeCompleteReturnType(m_main, to_method); 1175 } 1176 1177 clang::Decl * 1178 ClangASTImporter::ASTImporterDelegate::GetOriginalDecl(clang::Decl *To) { 1179 return m_main.GetDeclOrigin(To).decl; 1180 } 1181