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
Record(const RecordDecl * Decl,BaseList && SrcBases,FieldList && SrcFields,VirtualBaseList && SrcVirtualBases,unsigned VirtualSize,unsigned BaseSize)15 Record::Record(const RecordDecl *Decl, BaseList &&SrcBases,
16 FieldList &&SrcFields, VirtualBaseList &&SrcVirtualBases,
17 unsigned VirtualSize, unsigned BaseSize)
18 : Decl(Decl), Bases(std::move(SrcBases)), Fields(std::move(SrcFields)),
19 BaseSize(BaseSize), VirtualSize(VirtualSize), IsUnion(Decl->isUnion()) {
20 for (Base &V : SrcVirtualBases)
21 VirtualBases.push_back({ V.Decl, V.Offset + BaseSize, V.Desc, V.R });
22
23 for (Base &B : Bases)
24 BaseMap[B.Decl] = &B;
25 for (Field &F : Fields)
26 FieldMap[F.Decl] = &F;
27 for (Base &V : VirtualBases)
28 VirtualBaseMap[V.Decl] = &V;
29 }
30
getName() const31 const std::string Record::getName() const {
32 std::string Ret;
33 llvm::raw_string_ostream OS(Ret);
34 Decl->getNameForDiagnostic(OS, Decl->getASTContext().getPrintingPolicy(),
35 /*Qualified=*/true);
36 return Ret;
37 }
38
getField(const FieldDecl * FD) const39 const Record::Field *Record::getField(const FieldDecl *FD) const {
40 auto It = FieldMap.find(FD);
41 assert(It != FieldMap.end() && "Missing field");
42 return It->second;
43 }
44
getBase(const RecordDecl * FD) const45 const Record::Base *Record::getBase(const RecordDecl *FD) const {
46 auto It = BaseMap.find(FD);
47 assert(It != BaseMap.end() && "Missing base");
48 return It->second;
49 }
50
getBase(QualType T) const51 const Record::Base *Record::getBase(QualType T) const {
52 if (auto *RT = T->getAs<RecordType>()) {
53 const RecordDecl *RD = RT->getDecl();
54 return BaseMap.lookup(RD);
55 }
56 return nullptr;
57 }
58
getVirtualBase(const RecordDecl * FD) const59 const Record::Base *Record::getVirtualBase(const RecordDecl *FD) const {
60 auto It = VirtualBaseMap.find(FD);
61 assert(It != VirtualBaseMap.end() && "Missing virtual base");
62 return It->second;
63 }
64