InterpState.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 inConstantContext() const { return Parent.InConstantContext; }
  61. bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }
  62. void setActiveDiagnostic(bool Flag) override {
  63. Parent.setActiveDiagnostic(Flag);
  64. }
  65. void setFoldFailureDiagnostic(bool Flag) override {
  66. Parent.setFoldFailureDiagnostic(Flag);
  67. }
  68. bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }
  69. /// Reports overflow and return true if evaluation should continue.
  70. bool reportOverflow(const Expr *E, const llvm::APSInt &Value);
  71. /// Deallocates a pointer.
  72. void deallocate(Block *B);
  73. /// Delegates source mapping to the mapper.
  74. SourceInfo getSource(const Function *F, CodePtr PC) const override {
  75. return M ? M->getSource(F, PC) : F->getSource(PC);
  76. }
  77. private:
  78. /// AST Walker state.
  79. State &Parent;
  80. /// Dead block chain.
  81. DeadBlock *DeadBlocks = nullptr;
  82. /// Reference to the offset-source mapping.
  83. SourceMapper *M;
  84. public:
  85. /// Reference to the module containing all bytecode.
  86. Program &P;
  87. /// Temporary stack.
  88. InterpStack &Stk;
  89. /// Interpreter Context.
  90. Context &Ctx;
  91. /// The current frame.
  92. InterpFrame *Current = nullptr;
  93. /// Call stack depth.
  94. unsigned CallStackDepth;
  95. };
  96. } // namespace interp
  97. } // namespace clang
  98. #endif