| 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, BaseList &&SrcBases, |
| 16 | FieldList &&SrcFields, VirtualBaseList &&SrcVirtualBases, |
| 17 | unsigned VirtualSize, unsigned BaseSize, bool HasPtrField) |
| 18 | : Decl(Decl), Bases(std::move(SrcBases)), Fields(std::move(SrcFields)), |
| 19 | BaseSize(BaseSize), VirtualSize(VirtualSize), IsUnion(Decl->isUnion()), |
| 20 | IsAnonymousUnion(IsUnion && Decl->isAnonymousStructOrUnion()), |
| 21 | HasPtrField(HasPtrField) { |
| 22 | for (Base &V : SrcVirtualBases) |
| 23 | VirtualBases.emplace_back(Args&: V.Decl, Args&: V.Desc, Args&: V.R, Args: V.Offset + BaseSize); |
| 24 | |
| 25 | for (Base &B : Bases) { |
| 26 | BaseMap[B.Decl] = &B; |
| 27 | if (!this->HasPtrField) |
| 28 | this->HasPtrField |= B.R->hasPtrField(); |
| 29 | } |
| 30 | for (Base &V : VirtualBases) { |
| 31 | VirtualBaseMap[V.Decl] = &V; |
| 32 | if (!this->HasPtrField) |
| 33 | this->HasPtrField |= V.R->hasPtrField(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | std::string Record::getName() const { |
| 38 | std::string Ret; |
| 39 | llvm::raw_string_ostream OS(Ret); |
| 40 | Decl->getNameForDiagnostic(OS, Policy: Decl->getASTContext().getPrintingPolicy(), |
| 41 | /*Qualified=*/true); |
| 42 | return Ret; |
| 43 | } |
| 44 | |
| 45 | bool Record::hasTrivialDtor() const { |
| 46 | if (isAnonymousUnion()) |
| 47 | return true; |
| 48 | const CXXDestructorDecl *Dtor = getDestructor(); |
| 49 | return !Dtor || Dtor->isTrivial(); |
| 50 | } |
| 51 | |
| 52 | const Record::Base *Record::getBase(const RecordDecl *FD) const { |
| 53 | auto It = BaseMap.find(Val: FD); |
| 54 | assert(It != BaseMap.end() && "Missing base" ); |
| 55 | return It->second; |
| 56 | } |
| 57 | |
| 58 | const Record::Base *Record::getBase(QualType T) const { |
| 59 | if (auto *RD = T->getAsCXXRecordDecl()) |
| 60 | return BaseMap.lookup(Val: RD); |
| 61 | return nullptr; |
| 62 | } |
| 63 | |
| 64 | const Record::Base *Record::getVirtualBase(const RecordDecl *FD) const { |
| 65 | auto It = VirtualBaseMap.find(Val: FD); |
| 66 | assert(It != VirtualBaseMap.end() && "Missing virtual base" ); |
| 67 | return It->second; |
| 68 | } |
| 69 | |