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