1//===------------------------- MemberPointer.cpp ----------------*- 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 "MemberPointer.h"
10#include "Context.h"
11#include "Program.h"
12#include "Record.h"
13
14namespace clang {
15namespace interp {
16
17std::optional<Pointer> MemberPointer::toPointer(const Context &Ctx) const {
18 if (!getDecl() || isa<FunctionDecl>(Val: getDecl()))
19 return Base;
20 assert((isa<FieldDecl, IndirectFieldDecl>(getDecl())));
21
22 if (!Base.isBlockPointer())
23 return std::nullopt;
24
25 if (PtrOffset >= 0) {
26 // If the resulting base would be too small, return nullopt.
27 if (Base.BS.Base < static_cast<unsigned>(PtrOffset))
28 return std::nullopt;
29 }
30
31 Pointer CastedBase =
32 (PtrOffset < 0 ? Base.atField(Off: -PtrOffset) : Base.atFieldSub(Off: PtrOffset));
33
34 const Record *BaseRecord = CastedBase.getRecord();
35 if (!BaseRecord)
36 return std::nullopt;
37
38 unsigned Offset = 0;
39
40 if (const auto *FD = dyn_cast<FieldDecl>(Val: getDecl())) {
41 if (FD->getParent() == BaseRecord->getDecl())
42 return CastedBase.atField(Off: BaseRecord->getField(FD)->Offset);
43
44 const RecordDecl *FieldParent = FD->getParent();
45 const Record *FieldRecord = Ctx.getRecord(D: FieldParent);
46
47 Offset += FieldRecord->getField(FD)->Offset;
48 if (Offset > CastedBase.block()->getSize())
49 return std::nullopt;
50
51 if (const RecordDecl *BaseDecl = Base.getDeclPtr().getRecord()->getDecl();
52 BaseDecl != FieldParent)
53 Offset += Ctx.collectBaseOffset(BaseDecl: FieldParent, DerivedDecl: BaseDecl);
54
55 } else {
56 const auto *IFD = cast<IndirectFieldDecl>(Val: getDecl());
57
58 for (const NamedDecl *ND : IFD->chain()) {
59 const FieldDecl *F = cast<FieldDecl>(Val: ND);
60 const RecordDecl *FieldParent = F->getParent();
61 const Record *FieldRecord = Ctx.getRecord(D: FieldParent);
62 Offset += FieldRecord->getField(FD: F)->Offset;
63 }
64 }
65
66 assert(BaseRecord);
67 if (Offset > CastedBase.block()->getSize())
68 return std::nullopt;
69
70 assert(Offset <= CastedBase.block()->getSize());
71 return Pointer(const_cast<Block *>(Base.block()), Offset, Offset);
72}
73
74APValue MemberPointer::toAPValue(const ASTContext &ASTCtx) const {
75 if (isZero())
76 return APValue(static_cast<ValueDecl *>(nullptr), /*IsDerivedMember=*/false,
77 /*Path=*/{});
78
79 if (hasBase())
80 return Base.toAPValue(ASTCtx);
81
82 return APValue(getDecl(), /*IsDerivedMember=*/isDerivedMember(),
83 /*Path=*/ArrayRef(Path, PathLength));
84}
85
86ComparisonCategoryResult
87MemberPointer::compare(const MemberPointer &RHS) const {
88 assert(!isZero());
89 assert(!RHS.isZero());
90
91 const auto getCmpDecl = [](const MemberPointer &P) -> const Decl * {
92 const Decl *D = P.getDecl()->getMostRecentDecl();
93 if (const auto *FD = dyn_cast<FieldDecl>(Val: D))
94 D = FD->getFirstDecl();
95 return D;
96 };
97
98 const Decl *LHSCmpDecl = getCmpDecl(*this);
99 const Decl *RHSCmpDecl = getCmpDecl(RHS);
100
101 if (LHSCmpDecl == RHSCmpDecl) {
102 if (this->PathLength != RHS.PathLength)
103 return ComparisonCategoryResult::Unordered;
104
105 if (PathLength != 0 &&
106 std::memcmp(s1: Path, s2: RHS.Path, n: PathLength * sizeof(CXXRecordDecl *)) != 0)
107 return ComparisonCategoryResult::Unordered;
108
109 return ComparisonCategoryResult::Equal;
110 }
111 return ComparisonCategoryResult::Unordered;
112}
113
114} // namespace interp
115} // namespace clang
116