Path: blob/main/contrib/llvm-project/clang/lib/AST/Interp/InterpState.h
35292 views
//===--- InterpState.h - Interpreter state 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//===----------------------------------------------------------------------===//7//8// Definition of the interpreter state and entry point.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_CLANG_AST_INTERP_INTERPSTATE_H13#define LLVM_CLANG_AST_INTERP_INTERPSTATE_H1415#include "Context.h"16#include "DynamicAllocator.h"17#include "Function.h"18#include "InterpFrame.h"19#include "InterpStack.h"20#include "State.h"21#include "clang/AST/APValue.h"22#include "clang/AST/ASTDiagnostic.h"23#include "clang/AST/Expr.h"24#include "clang/AST/OptionalDiagnostic.h"2526namespace clang {27namespace interp {28class Context;29class Function;30class InterpStack;31class InterpFrame;32class SourceMapper;3334/// Interpreter context.35class InterpState final : public State, public SourceMapper {36public:37InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx,38SourceMapper *M = nullptr);3940~InterpState();4142void cleanup();4344InterpState(const InterpState &) = delete;45InterpState &operator=(const InterpState &) = delete;4647// Stack frame accessors.48Frame *getSplitFrame() { return Parent.getCurrentFrame(); }49Frame *getCurrentFrame() override;50unsigned getCallStackDepth() override {51return Current ? (Current->getDepth() + 1) : 1;52}53const Frame *getBottomFrame() const override {54return Parent.getBottomFrame();55}5657// Access objects from the walker context.58Expr::EvalStatus &getEvalStatus() const override {59return Parent.getEvalStatus();60}61ASTContext &getCtx() const override { return Parent.getCtx(); }6263// Forward status checks and updates to the walker.64bool checkingForUndefinedBehavior() const override {65return Parent.checkingForUndefinedBehavior();66}67bool keepEvaluatingAfterFailure() const override {68return Parent.keepEvaluatingAfterFailure();69}70bool checkingPotentialConstantExpression() const override {71return Parent.checkingPotentialConstantExpression();72}73bool noteUndefinedBehavior() override {74return Parent.noteUndefinedBehavior();75}76bool inConstantContext() const { return Parent.InConstantContext; }77bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }78void setActiveDiagnostic(bool Flag) override {79Parent.setActiveDiagnostic(Flag);80}81void setFoldFailureDiagnostic(bool Flag) override {82Parent.setFoldFailureDiagnostic(Flag);83}84bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }8586/// Reports overflow and return true if evaluation should continue.87bool reportOverflow(const Expr *E, const llvm::APSInt &Value);8889/// Deallocates a pointer.90void deallocate(Block *B);9192/// Delegates source mapping to the mapper.93SourceInfo getSource(const Function *F, CodePtr PC) const override {94if (M)95return M->getSource(F, PC);9697assert(F && "Function cannot be null");98return F->getSource(PC);99}100101Context &getContext() const { return Ctx; }102103void setEvalLocation(SourceLocation SL) { this->EvalLocation = SL; }104105DynamicAllocator &getAllocator() { return Alloc; }106107/// Diagnose any dynamic allocations that haven't been freed yet.108/// Will return \c false if there were any allocations to diagnose,109/// \c true otherwise.110bool maybeDiagnoseDanglingAllocations();111112private:113friend class EvaluationResult;114/// AST Walker state.115State &Parent;116/// Dead block chain.117DeadBlock *DeadBlocks = nullptr;118/// Reference to the offset-source mapping.119SourceMapper *M;120/// Allocator used for dynamic allocations performed via the program.121DynamicAllocator Alloc;122123public:124/// Reference to the module containing all bytecode.125Program &P;126/// Temporary stack.127InterpStack &Stk;128/// Interpreter Context.129Context &Ctx;130/// The current frame.131InterpFrame *Current = nullptr;132/// Source location of the evaluating expression133SourceLocation EvalLocation;134/// Declaration we're initializing/evaluting, if any.135const VarDecl *EvaluatingDecl = nullptr;136137llvm::SmallVector<138std::pair<const Expr *, const LifetimeExtendedTemporaryDecl *>>139SeenGlobalTemporaries;140};141142} // namespace interp143} // namespace clang144145#endif146147148