Path: blob/main/contrib/llvm-project/clang/lib/AST/ByteCode/InterpState.cpp
213799 views
//===--- InterpState.cpp - Interpreter for the constexpr VM -----*- 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 "InterpState.h"9#include "InterpFrame.h"10#include "InterpStack.h"11#include "Program.h"12#include "State.h"1314using namespace clang;15using namespace clang::interp;1617InterpState::InterpState(State &Parent, Program &P, InterpStack &Stk,18Context &Ctx, SourceMapper *M)19: Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), BottomFrame(*this),20Current(&BottomFrame) {}2122InterpState::InterpState(State &Parent, Program &P, InterpStack &Stk,23Context &Ctx, const Function *Func)24: Parent(Parent), M(nullptr), P(P), Stk(Stk), Ctx(Ctx),25BottomFrame(*this, Func, nullptr, CodePtr(), Func->getArgSize()),26Current(&BottomFrame) {}2728bool InterpState::inConstantContext() const {29if (ConstantContextOverride)30return *ConstantContextOverride;3132return Parent.InConstantContext;33}3435InterpState::~InterpState() {36while (Current && !Current->isBottomFrame()) {37InterpFrame *Next = Current->Caller;38delete Current;39Current = Next;40}41BottomFrame.destroyScopes();4243while (DeadBlocks) {44DeadBlock *Next = DeadBlocks->Next;45std::free(DeadBlocks);46DeadBlocks = Next;47}48}4950void InterpState::cleanup() {51// As a last resort, make sure all pointers still pointing to a dead block52// don't point to it anymore.53for (DeadBlock *DB = DeadBlocks; DB; DB = DB->Next) {54for (Pointer *P = DB->B.Pointers; P; P = P->Next) {55P->PointeeStorage.BS.Pointee = nullptr;56}57}5859Alloc.cleanup();60}6162Frame *InterpState::getCurrentFrame() {63if (Current && Current->Caller)64return Current;65return Parent.getCurrentFrame();66}6768bool InterpState::reportOverflow(const Expr *E, const llvm::APSInt &Value) {69QualType Type = E->getType();70CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;71return noteUndefinedBehavior();72}7374void InterpState::deallocate(Block *B) {75assert(B);76const Descriptor *Desc = B->getDescriptor();77assert(Desc);7879if (B->hasPointers()) {80size_t Size = B->getSize();8182// Allocate a new block, transferring over pointers.83char *Memory =84reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size));85auto *D = new (Memory) DeadBlock(DeadBlocks, B);86std::memset(D->B.rawData(), 0, D->B.getSize());8788// Move data and metadata from the old block to the new (dead)block.89if (B->IsInitialized && Desc->MoveFn) {90Desc->MoveFn(B, B->data(), D->data(), Desc);91if (Desc->getMetadataSize() > 0)92std::memcpy(D->rawData(), B->rawData(), Desc->getMetadataSize());93}94D->B.IsInitialized = B->IsInitialized;9596// We moved the contents over to the DeadBlock.97B->IsInitialized = false;98} else if (B->IsInitialized) {99B->invokeDtor();100}101}102103bool InterpState::maybeDiagnoseDanglingAllocations() {104bool NoAllocationsLeft = (Alloc.getNumAllocations() == 0);105106if (!checkingPotentialConstantExpression()) {107for (const auto &It : Alloc.allocation_sites()) {108assert(It.second.size() > 0);109110const Expr *Source = It.first;111CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)112<< (It.second.size() - 1) << Source->getSourceRange();113}114}115// Keep evaluating before C++20, since the CXXNewExpr wasn't valid there116// in the first place.117return NoAllocationsLeft || !getLangOpts().CPlusPlus20;118}119120StdAllocatorCaller InterpState::getStdAllocatorCaller(StringRef Name) const {121for (const InterpFrame *F = Current; F; F = F->Caller) {122const Function *Func = F->getFunction();123if (!Func)124continue;125const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Func->getDecl());126if (!MD)127continue;128const IdentifierInfo *FnII = MD->getIdentifier();129if (!FnII || !FnII->isStr(Name))130continue;131132const auto *CTSD =133dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent());134if (!CTSD)135continue;136137const IdentifierInfo *ClassII = CTSD->getIdentifier();138const TemplateArgumentList &TAL = CTSD->getTemplateArgs();139if (CTSD->isInStdNamespace() && ClassII && ClassII->isStr("allocator") &&140TAL.size() >= 1 && TAL[0].getKind() == TemplateArgument::Type) {141QualType ElemType = TAL[0].getAsType();142const auto *NewCall = cast<CallExpr>(F->Caller->getExpr(F->getRetPC()));143return {NewCall, ElemType};144}145}146147return {};148}149150151