| 528 | } |
| 529 | |
| 530 | void AnalysisAction::CoverReferencedTypes(clang::ASTContext& context) { |
| 531 | // Add common fixed-size integer types explicitly |
| 532 | for (unsigned size : {8, 32, 64}) { |
| 533 | types.emplace(context.getIntTypeForBitwidth(size, false).getTypePtr(), RepackedType {}); |
| 534 | types.emplace(context.getIntTypeForBitwidth(size, true).getTypePtr(), RepackedType {}); |
| 535 | } |
| 536 | |
| 537 | // Repeat until no more children are appended |
| 538 | for (bool changed = true; std::exchange(changed, false);) { |
| 539 | for (auto next_type_it = types.begin(), type_it = next_type_it; type_it != types.end(); type_it = next_type_it) { |
| 540 | ++next_type_it; |
| 541 | const auto& [type, type_repack_info] = *type_it; |
| 542 | if (!type->isStructureType()) { |
| 543 | continue; |
| 544 | } |
| 545 | |
| 546 | if (type_repack_info.assumed_compatible) { |
| 547 | // If assumed compatible, we don't need the member definitions |
| 548 | continue; |
| 549 | } |
| 550 | |
| 551 | for (auto* member : type->getAsStructureType()->getDecl()->fields()) { |
| 552 | auto member_type = member->getType().getTypePtr(); |
| 553 | if (type_repack_info.UsesCustomRepackFor(member) && member_type->isPointerType() && member_type->getPointeeType()->isStructureType()) { |
| 554 | continue; |
| 555 | } |
| 556 | |
| 557 | while (member_type->isArrayType()) { |
| 558 | member_type = member_type->getArrayElementTypeNoTypeQual(); |
| 559 | } |
| 560 | while (member_type->isPointerType()) { |
| 561 | member_type = member_type->getPointeeType().getTypePtr(); |
| 562 | } |
| 563 | |
| 564 | if (!member_type->isBuiltinType()) { |
| 565 | member_type = context.getCanonicalType(member_type); |
| 566 | } |
| 567 | if (types.contains(member_type) && types.at(member_type).pointers_only) { |
| 568 | if (member_type == context.getCanonicalType(member->getType().getTypePtr())) { |
| 569 | throw std::runtime_error( |
| 570 | fmt::format("\"{}\" references opaque type \"{}\" via non-pointer member \"{}\"", clang::QualType {type, 0}.getAsString(), |
| 571 | clang::QualType {member_type, 0}.getAsString(), member->getNameAsString())); |
| 572 | } |
| 573 | continue; |
| 574 | } |
| 575 | if (member_type->isUnionType() && !types.contains(member_type) && !type_repack_info.UsesCustomRepackFor(member)) { |
| 576 | throw std::runtime_error(fmt::format("\"{}\" has unannotated member \"{}\" of union type \"{}\"", clang::QualType {type, 0}.getAsString(), |
| 577 | member->getNameAsString(), clang::QualType {member_type, 0}.getAsString())); |
| 578 | } |
| 579 | |
| 580 | if (!member_type->isStructureType() && !(member_type->isBuiltinType() && !member_type->isVoidType()) && !member_type->isEnumeralType()) { |
| 581 | continue; |
| 582 | } |
| 583 | |
| 584 | auto [new_type_it, inserted] = types.emplace(member_type, RepackedType {}); |
| 585 | if (inserted) { |
| 586 | changed = true; |
| 587 | next_type_it = new_type_it; |