Path: blob/main/contrib/llvm-project/clang/lib/AST/Interp/InterpBlock.cpp
35291 views
//===--- Block.cpp - Allocated blocks for the interpreter -------*- 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//===----------------------------------------------------------------------===//7//8// Defines the classes describing allocated blocks.9//10//===----------------------------------------------------------------------===//1112#include "InterpBlock.h"13#include "Pointer.h"1415using namespace clang;16using namespace clang::interp;1718void Block::addPointer(Pointer *P) {19assert(P);20if (IsStatic) {21assert(!Pointers);22return;23}2425#ifndef NDEBUG26assert(!hasPointer(P));27#endif28if (Pointers)29Pointers->Prev = P;30P->Next = Pointers;31P->Prev = nullptr;32Pointers = P;33}3435void Block::removePointer(Pointer *P) {36assert(P);37if (IsStatic) {38assert(!Pointers);39return;40}4142#ifndef NDEBUG43assert(hasPointer(P));44#endif4546if (Pointers == P)47Pointers = P->Next;4849if (P->Prev)50P->Prev->Next = P->Next;51if (P->Next)52P->Next->Prev = P->Prev;53}5455void Block::cleanup() {56if (Pointers == nullptr && IsDead)57(reinterpret_cast<DeadBlock *>(this + 1) - 1)->free();58}5960void Block::replacePointer(Pointer *Old, Pointer *New) {61assert(Old);62assert(New);63if (IsStatic) {64assert(!Pointers);65return;66}6768#ifndef NDEBUG69assert(hasPointer(Old));70#endif7172removePointer(Old);73addPointer(New);7475Old->PointeeStorage.BS.Pointee = nullptr;7677#ifndef NDEBUG78assert(!hasPointer(Old));79assert(hasPointer(New));80#endif81}8283#ifndef NDEBUG84bool Block::hasPointer(const Pointer *P) const {85for (const Pointer *C = Pointers; C; C = C->Next) {86if (C == P)87return true;88}89return false;90}91#endif9293DeadBlock::DeadBlock(DeadBlock *&Root, Block *Blk)94: Root(Root),95B(~0u, Blk->Desc, Blk->IsStatic, Blk->IsExtern, /*isDead=*/true) {96// Add the block to the chain of dead blocks.97if (Root)98Root->Prev = this;99100Next = Root;101Prev = nullptr;102Root = this;103104// Transfer pointers.105B.Pointers = Blk->Pointers;106for (Pointer *P = Blk->Pointers; P; P = P->Next)107P->PointeeStorage.BS.Pointee = &B;108Blk->Pointers = nullptr;109}110111void DeadBlock::free() {112if (B.IsInitialized)113B.invokeDtor();114115if (Prev)116Prev->Next = Next;117if (Next)118Next->Prev = Prev;119if (Root == this)120Root = Next;121std::free(this);122}123124125