InterpState.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===--- InterpState.h - Interpreter state for the constexpr VM -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Definition of the interpreter state and entry point.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_AST_INTERP_INTERPSTATE_H
  13. #define LLVM_CLANG_AST_INTERP_INTERPSTATE_H
  14. #include "Context.h"
  15. #include "Function.h"
  16. #include "InterpStack.h"
  17. #include "State.h"
  18. #include "clang/AST/APValue.h"
  19. #include "clang/AST/ASTDiagnostic.h"
  20. #include "clang/AST/Expr.h"
  21. #include "clang/AST/OptionalDiagnostic.h"
  22. namespace clang {
  23. namespace interp {
  24. class Context;
  25. class Function;
  26. class InterpStack;
  27. class InterpFrame;
  28. class SourceMapper;
  29. /// Interpreter context.
  30. class InterpState final : public State, public SourceMapper {
  31. public:
  32. InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx,
  33. SourceMapper *M = nullptr);
  34. ~InterpState();
  35. // Stack frame accessors.
  36. Frame *getSplitFrame() { return Parent.getCurrentFrame(); }
  37. Frame *getCurrentFrame() override;
  38. unsigned getCallStackDepth() override { return CallStackDepth; }
  39. const Frame *getBottomFrame() const override {
  40. return Parent.getBottomFrame();
  41. }
  42. // Access objects from the walker context.
  43. Expr::EvalStatus &getEvalStatus() const override {
  44. return Parent.getEvalStatus();
  45. }
  46. ASTContext &getCtx() const override { return Parent.getCtx(); }
  47. // Forward status checks and updates to the walker.
  48. bool checkingForUndefinedBehavior() const override {
  49. return Parent.checkingForUndefinedBehavior();
  50. }
  51. bool keepEvaluatingAfterFailure() const override {
  52. return Parent.keepEvaluatingAfterFailure();
  53. }
  54. bool checkingPotentialConstantExpression() const override {
  55. return Parent.checkingPotentialConstantExpression();
  56. }
  57. bool noteUndefinedBehavior() override {
  58. return Parent.noteUndefinedBehavior();
  59. }
  60. bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }
  61. void setActiveDiagnostic(bool Flag) override {
  62. Parent.setActiveDiagnostic(Flag);
  63. }
  64. void setFoldFailureDiagnostic(bool Flag) override {
  65. Parent.setFoldFailureDiagnostic(Flag);
  66. }
  67. bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }
  68. /// Reports overflow and return true if evaluation should continue.
  69. bool reportOverflow(const Expr *E, const llvm::APSInt &Value);
  70. /// Deallocates a pointer.
  71. void deallocate(Block *B);
  72. /// Delegates source mapping to the mapper.
  73. SourceInfo getSource(Function *F, CodePtr PC) const override {
  74. return M ? M->getSource(F, PC) : F->getSource(PC);
  75. }
  76. private:
  77. /// AST Walker state.
  78. State &Parent;
  79. /// Dead block chain.
  80. DeadBlock *DeadBlocks = nullptr;
  81. /// Reference to the offset-source mapping.
  82. SourceMapper *M;
  83. public:
  84. /// Reference to the module containing all bytecode.
  85. Program &P;
  86. /// Temporary stack.
  87. InterpStack &Stk;
  88. /// Interpreter Context.
  89. Context &Ctx;
  90. /// The current frame.
  91. InterpFrame *Current = nullptr;
  92. /// Call stack depth.
  93. unsigned CallStackDepth;
  94. };
  95. } // namespace interp
  96. } // namespace clang
  97. #endif