1353942Sdim//===--- InterpState.h - Interpreter state for the constexpr VM -*- C++ -*-===//
2353942Sdim//
3353942Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353942Sdim// See https://llvm.org/LICENSE.txt for license information.
5353942Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6353942Sdim//
7353942Sdim//===----------------------------------------------------------------------===//
8353942Sdim//
9353942Sdim// Definition of the interpreter state and entry point.
10353942Sdim//
11353942Sdim//===----------------------------------------------------------------------===//
12353942Sdim
13353942Sdim#ifndef LLVM_CLANG_AST_INTERP_INTERPSTATE_H
14353942Sdim#define LLVM_CLANG_AST_INTERP_INTERPSTATE_H
15353942Sdim
16353942Sdim#include "Context.h"
17353942Sdim#include "Function.h"
18353942Sdim#include "InterpStack.h"
19353942Sdim#include "State.h"
20353942Sdim#include "clang/AST/APValue.h"
21353942Sdim#include "clang/AST/ASTDiagnostic.h"
22353942Sdim#include "clang/AST/Expr.h"
23353942Sdim#include "clang/AST/OptionalDiagnostic.h"
24353942Sdim
25353942Sdimnamespace clang {
26353942Sdimnamespace interp {
27353942Sdimclass Context;
28353942Sdimclass Function;
29353942Sdimclass InterpStack;
30353942Sdimclass InterpFrame;
31353942Sdimclass SourceMapper;
32353942Sdim
33353942Sdim/// Interpreter context.
34353942Sdimclass InterpState final : public State, public SourceMapper {
35353942Sdimpublic:
36353942Sdim  InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx,
37353942Sdim              SourceMapper *M = nullptr);
38353942Sdim
39353942Sdim  ~InterpState();
40353942Sdim
41353942Sdim  // Stack frame accessors.
42353942Sdim  Frame *getSplitFrame() { return Parent.getCurrentFrame(); }
43353942Sdim  Frame *getCurrentFrame() override;
44353942Sdim  unsigned getCallStackDepth() override { return CallStackDepth; }
45353942Sdim  const Frame *getBottomFrame() const override {
46353942Sdim    return Parent.getBottomFrame();
47353942Sdim  }
48353942Sdim
49353942Sdim  // Acces objects from the walker context.
50353942Sdim  Expr::EvalStatus &getEvalStatus() const override {
51353942Sdim    return Parent.getEvalStatus();
52353942Sdim  }
53353942Sdim  ASTContext &getCtx() const override { return Parent.getCtx(); }
54353942Sdim
55353942Sdim  // Forward status checks and updates to the walker.
56353942Sdim  bool checkingForUndefinedBehavior() const override {
57353942Sdim    return Parent.checkingForUndefinedBehavior();
58353942Sdim  }
59353942Sdim  bool keepEvaluatingAfterFailure() const override {
60353942Sdim    return Parent.keepEvaluatingAfterFailure();
61353942Sdim  }
62353942Sdim  bool checkingPotentialConstantExpression() const override {
63353942Sdim    return Parent.checkingPotentialConstantExpression();
64353942Sdim  }
65353942Sdim  bool noteUndefinedBehavior() override {
66353942Sdim    return Parent.noteUndefinedBehavior();
67353942Sdim  }
68353942Sdim  bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }
69353942Sdim  void setActiveDiagnostic(bool Flag) override {
70353942Sdim    Parent.setActiveDiagnostic(Flag);
71353942Sdim  }
72353942Sdim  void setFoldFailureDiagnostic(bool Flag) override {
73353942Sdim    Parent.setFoldFailureDiagnostic(Flag);
74353942Sdim  }
75353942Sdim  bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }
76353942Sdim
77353942Sdim  /// Reports overflow and return true if evaluation should continue.
78353942Sdim  bool reportOverflow(const Expr *E, const llvm::APSInt &Value);
79353942Sdim
80353942Sdim  /// Deallocates a pointer.
81353942Sdim  void deallocate(Block *B);
82353942Sdim
83353942Sdim  /// Delegates source mapping to the mapper.
84353942Sdim  SourceInfo getSource(Function *F, CodePtr PC) const override {
85353942Sdim    return M ? M->getSource(F, PC) : F->getSource(PC);
86353942Sdim  }
87353942Sdim
88353942Sdimprivate:
89353942Sdim  /// AST Walker state.
90353942Sdim  State &Parent;
91353942Sdim  /// Dead block chain.
92353942Sdim  DeadBlock *DeadBlocks = nullptr;
93353942Sdim  /// Reference to the offset-source mapping.
94353942Sdim  SourceMapper *M;
95353942Sdim
96353942Sdimpublic:
97353942Sdim  /// Reference to the module containing all bytecode.
98353942Sdim  Program &P;
99353942Sdim  /// Temporary stack.
100353942Sdim  InterpStack &Stk;
101353942Sdim  /// Interpreter Context.
102353942Sdim  Context &Ctx;
103353942Sdim  /// The current frame.
104353942Sdim  InterpFrame *Current = nullptr;
105353942Sdim  /// Call stack depth.
106353942Sdim  unsigned CallStackDepth;
107353942Sdim};
108353942Sdim
109353942Sdim} // namespace interp
110353942Sdim} // namespace clang
111353942Sdim
112353942Sdim#endif
113