xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/Interp/Record.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1a7dea167SDimitry Andric //===--- Record.cpp - struct and class metadata for the VM ------*- C++ -*-===//
2a7dea167SDimitry Andric //
3a7dea167SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a7dea167SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5a7dea167SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a7dea167SDimitry Andric //
7a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
8a7dea167SDimitry Andric 
9a7dea167SDimitry Andric #include "Record.h"
10*0fca6ea1SDimitry Andric #include "clang/AST/ASTContext.h"
11a7dea167SDimitry Andric 
12a7dea167SDimitry Andric using namespace clang;
13a7dea167SDimitry Andric using namespace clang::interp;
14a7dea167SDimitry Andric 
15a7dea167SDimitry Andric Record::Record(const RecordDecl *Decl, BaseList &&SrcBases,
16a7dea167SDimitry Andric                FieldList &&SrcFields, VirtualBaseList &&SrcVirtualBases,
17a7dea167SDimitry Andric                unsigned VirtualSize, unsigned BaseSize)
18a7dea167SDimitry Andric     : Decl(Decl), Bases(std::move(SrcBases)), Fields(std::move(SrcFields)),
19*0fca6ea1SDimitry Andric       BaseSize(BaseSize), VirtualSize(VirtualSize), IsUnion(Decl->isUnion()) {
20a7dea167SDimitry Andric   for (Base &V : SrcVirtualBases)
21a7dea167SDimitry Andric     VirtualBases.push_back({ V.Decl, V.Offset + BaseSize, V.Desc, V.R });
22a7dea167SDimitry Andric 
23a7dea167SDimitry Andric   for (Base &B : Bases)
24a7dea167SDimitry Andric     BaseMap[B.Decl] = &B;
25a7dea167SDimitry Andric   for (Field &F : Fields)
26a7dea167SDimitry Andric     FieldMap[F.Decl] = &F;
27a7dea167SDimitry Andric   for (Base &V : VirtualBases)
28a7dea167SDimitry Andric     VirtualBaseMap[V.Decl] = &V;
29a7dea167SDimitry Andric }
30a7dea167SDimitry Andric 
31*0fca6ea1SDimitry Andric const std::string Record::getName() const {
32*0fca6ea1SDimitry Andric   std::string Ret;
33*0fca6ea1SDimitry Andric   llvm::raw_string_ostream OS(Ret);
34*0fca6ea1SDimitry Andric   Decl->getNameForDiagnostic(OS, Decl->getASTContext().getPrintingPolicy(),
35*0fca6ea1SDimitry Andric                              /*Qualified=*/true);
36*0fca6ea1SDimitry Andric   return Ret;
37*0fca6ea1SDimitry Andric }
38*0fca6ea1SDimitry Andric 
39a7dea167SDimitry Andric const Record::Field *Record::getField(const FieldDecl *FD) const {
40a7dea167SDimitry Andric   auto It = FieldMap.find(FD);
41a7dea167SDimitry Andric   assert(It != FieldMap.end() && "Missing field");
42a7dea167SDimitry Andric   return It->second;
43a7dea167SDimitry Andric }
44a7dea167SDimitry Andric 
45a7dea167SDimitry Andric const Record::Base *Record::getBase(const RecordDecl *FD) const {
46a7dea167SDimitry Andric   auto It = BaseMap.find(FD);
47a7dea167SDimitry Andric   assert(It != BaseMap.end() && "Missing base");
48a7dea167SDimitry Andric   return It->second;
49a7dea167SDimitry Andric }
50a7dea167SDimitry Andric 
5106c3fb27SDimitry Andric const Record::Base *Record::getBase(QualType T) const {
52*0fca6ea1SDimitry Andric   if (auto *RT = T->getAs<RecordType>()) {
53*0fca6ea1SDimitry Andric     const RecordDecl *RD = RT->getDecl();
5406c3fb27SDimitry Andric     return BaseMap.lookup(RD);
5506c3fb27SDimitry Andric   }
56*0fca6ea1SDimitry Andric   return nullptr;
57*0fca6ea1SDimitry Andric }
5806c3fb27SDimitry Andric 
59a7dea167SDimitry Andric const Record::Base *Record::getVirtualBase(const RecordDecl *FD) const {
60a7dea167SDimitry Andric   auto It = VirtualBaseMap.find(FD);
61a7dea167SDimitry Andric   assert(It != VirtualBaseMap.end() && "Missing virtual base");
62a7dea167SDimitry Andric   return It->second;
63a7dea167SDimitry Andric }
64