Path: blob/main/contrib/llvm-project/clang/lib/AST/ByteCode/MemberPointer.cpp
213799 views
//===------------------------- MemberPointer.cpp ----------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "MemberPointer.h"9#include "Context.h"10#include "FunctionPointer.h"11#include "Program.h"12#include "Record.h"1314namespace clang {15namespace interp {1617std::optional<Pointer> MemberPointer::toPointer(const Context &Ctx) const {18if (!Dcl || isa<FunctionDecl>(Dcl))19return Base;20assert((isa<FieldDecl, IndirectFieldDecl>(Dcl)));2122if (!Base.isBlockPointer())23return std::nullopt;2425Pointer CastedBase =26(PtrOffset < 0 ? Base.atField(-PtrOffset) : Base.atFieldSub(PtrOffset));2728const Record *BaseRecord = CastedBase.getRecord();29if (!BaseRecord)30return std::nullopt;3132unsigned Offset = 0;33Offset += CastedBase.block()->getDescriptor()->getMetadataSize();3435if (const auto *FD = dyn_cast<FieldDecl>(Dcl)) {36if (FD->getParent() == BaseRecord->getDecl())37return CastedBase.atField(BaseRecord->getField(FD)->Offset);3839const RecordDecl *FieldParent = FD->getParent();40const Record *FieldRecord = Ctx.getRecord(FieldParent);4142Offset += FieldRecord->getField(FD)->Offset;43if (Offset > CastedBase.block()->getSize())44return std::nullopt;4546if (const RecordDecl *BaseDecl = Base.getDeclPtr().getRecord()->getDecl();47BaseDecl != FieldParent)48Offset += Ctx.collectBaseOffset(FieldParent, BaseDecl);4950} else {51const auto *IFD = cast<IndirectFieldDecl>(Dcl);5253for (const NamedDecl *ND : IFD->chain()) {54const FieldDecl *F = cast<FieldDecl>(ND);55const RecordDecl *FieldParent = F->getParent();56const Record *FieldRecord = Ctx.getRecord(FieldParent);57Offset += FieldRecord->getField(F)->Offset;58}59}6061assert(BaseRecord);62if (Offset > CastedBase.block()->getSize())63return std::nullopt;6465assert(Offset <= CastedBase.block()->getSize());66return Pointer(const_cast<Block *>(Base.block()), Offset, Offset);67}6869FunctionPointer MemberPointer::toFunctionPointer(const Context &Ctx) const {70return FunctionPointer(Ctx.getProgram().getFunction(cast<FunctionDecl>(Dcl)));71}7273APValue MemberPointer::toAPValue(const ASTContext &ASTCtx) const {74if (isZero())75return APValue(static_cast<ValueDecl *>(nullptr), /*IsDerivedMember=*/false,76/*Path=*/{});7778if (hasBase())79return Base.toAPValue(ASTCtx);8081return APValue(getDecl(), /*IsDerivedMember=*/false,82/*Path=*/{});83}8485} // namespace interp86} // namespace clang878889