| 1 | //===--- Record.cpp - struct and class metadata for the VM ------*- C++ -*-===// |
| 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 "Record.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | |
| 12 | using namespace clang; |
| 13 | using namespace clang::interp; |
| 14 | |
| 15 | Record::Record(const RecordDecl *Decl, ArrayRef<Base> Bases, |
| 16 | ArrayRef<Field> Fields, ArrayRef<Base> VirtualBases, |
| 17 | unsigned VirtualSize, unsigned BaseSize, bool HasPtrField) |
| 18 | : Decl(Decl), Bases(Bases), Fields(Fields), VirtualBases(VirtualBases), |
| 19 | BaseSize(BaseSize), VirtualSize(VirtualSize), IsUnion(Decl->isUnion()), |
| 20 | IsAnonymousUnion(IsUnion && Decl->isAnonymousStructOrUnion()), |
| 21 | HasPtrField(HasPtrField) { |
| 22 | for (const Base &B : this->Bases) |
| 23 | BaseMap[B.Decl] = &B; |
| 24 | } |
| 25 | |
| 26 | std::string Record::getName() const { |
| 27 | std::string Ret; |
| 28 | llvm::raw_string_ostream OS(Ret); |
| 29 | Decl->getNameForDiagnostic(OS, Policy: Decl->getASTContext().getPrintingPolicy(), |
| 30 | /*Qualified=*/true); |
| 31 | return Ret; |
| 32 | } |
| 33 | |
| 34 | bool Record::hasTrivialDtor() const { |
| 35 | if (isAnonymousUnion()) |
| 36 | return true; |
| 37 | const CXXDestructorDecl *Dtor = getDestructor(); |
| 38 | return !Dtor || Dtor->isTrivial(); |
| 39 | } |
| 40 | |
| 41 | const Record::Field *Record::findField(unsigned Offset) const { |
| 42 | if (auto It = llvm::find_if( |
| 43 | Range: Fields, |
| 44 | P: [=](const Record::Field &F) -> bool { return F.Offset == Offset; }); |
| 45 | It != Fields.end()) |
| 46 | return &*It; |
| 47 | return nullptr; |
| 48 | } |
| 49 | |
| 50 | const Record::Base *Record::getBase(const RecordDecl *RD) const { |
| 51 | auto It = BaseMap.find(Val: RD); |
| 52 | assert(It != BaseMap.end() && "Missing base" ); |
| 53 | return It->second; |
| 54 | } |
| 55 | |
| 56 | const Record::Base *Record::getBaseOrNull(const RecordDecl *RD) const { |
| 57 | return BaseMap.lookup(Val: RD); |
| 58 | } |
| 59 | |
| 60 | const Record::Base *Record::getBase(QualType T) const { |
| 61 | if (auto *RD = T->getAsCXXRecordDecl()) |
| 62 | return BaseMap.lookup(Val: RD); |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
| 66 | const Record::Base *Record::findBase(unsigned Offset) const { |
| 67 | if (auto It = llvm::find_if( |
| 68 | Range: Bases, |
| 69 | P: [=](const Record::Base &B) -> bool { return B.Offset == Offset; }); |
| 70 | It != Bases.end()) |
| 71 | return &*It; |
| 72 | return nullptr; |
| 73 | } |
| 74 | |
| 75 | const Record::Base *Record::findVirtualBase(const RecordDecl *FD) const { |
| 76 | if (auto *It = llvm::find_if( |
| 77 | Range: VirtualBases, |
| 78 | P: [=](const Record::Base &B) -> bool { return B.Decl == FD; }); |
| 79 | It != Bases.end()) |
| 80 | return &*It; |
| 81 | return nullptr; |
| 82 | } |
| 83 | |