Context.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //===--- Context.h - Context 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. // Defines the constexpr execution context.
  10. //
  11. // The execution context manages cached bytecode and the global context.
  12. // It invokes the compiler and interpreter, propagating errors.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CLANG_AST_INTERP_CONTEXT_H
  16. #define LLVM_CLANG_AST_INTERP_CONTEXT_H
  17. #include "InterpStack.h"
  18. #include "clang/AST/APValue.h"
  19. namespace clang {
  20. class ASTContext;
  21. class LangOptions;
  22. class FunctionDecl;
  23. class VarDecl;
  24. namespace interp {
  25. class Function;
  26. class Program;
  27. class State;
  28. enum PrimType : unsigned;
  29. /// Holds all information required to evaluate constexpr code in a module.
  30. class Context final {
  31. public:
  32. /// Initialises the constexpr VM.
  33. Context(ASTContext &Ctx);
  34. /// Cleans up the constexpr VM.
  35. ~Context();
  36. /// Checks if a function is a potential constant expression.
  37. bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl);
  38. /// Evaluates a toplevel expression as an rvalue.
  39. bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);
  40. /// Evaluates a toplevel initializer.
  41. bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result);
  42. /// Returns the AST context.
  43. ASTContext &getASTContext() const { return Ctx; }
  44. /// Returns the language options.
  45. const LangOptions &getLangOpts() const;
  46. /// Returns the interpreter stack.
  47. InterpStack &getStack() { return Stk; }
  48. /// Returns CHAR_BIT.
  49. unsigned getCharBit() const;
  50. /// Classifies an expression.
  51. std::optional<PrimType> classify(QualType T) const;
  52. private:
  53. /// Runs a function.
  54. bool Run(State &Parent, Function *Func, APValue &Result);
  55. /// Checks a result from the interpreter.
  56. bool Check(State &Parent, llvm::Expected<bool> &&R);
  57. /// Current compilation context.
  58. ASTContext &Ctx;
  59. /// Interpreter stack, shared across invocations.
  60. InterpStack Stk;
  61. /// Constexpr program.
  62. std::unique_ptr<Program> P;
  63. };
  64. } // namespace interp
  65. } // namespace clang
  66. #endif