1e5dd7070Spatrick //===--- Context.h - Context for the constexpr VM ---------------*- C++ -*-===// 2e5dd7070Spatrick // 3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information. 5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e5dd7070Spatrick // 7e5dd7070Spatrick //===----------------------------------------------------------------------===// 8e5dd7070Spatrick // 9e5dd7070Spatrick // Defines the constexpr execution context. 10e5dd7070Spatrick // 11e5dd7070Spatrick // The execution context manages cached bytecode and the global context. 12e5dd7070Spatrick // It invokes the compiler and interpreter, propagating errors. 13e5dd7070Spatrick // 14e5dd7070Spatrick //===----------------------------------------------------------------------===// 15e5dd7070Spatrick 16e5dd7070Spatrick #ifndef LLVM_CLANG_AST_INTERP_CONTEXT_H 17e5dd7070Spatrick #define LLVM_CLANG_AST_INTERP_CONTEXT_H 18e5dd7070Spatrick 19e5dd7070Spatrick #include "InterpStack.h" 20e5dd7070Spatrick #include "clang/AST/APValue.h" 21e5dd7070Spatrick 22e5dd7070Spatrick namespace clang { 23e5dd7070Spatrick class ASTContext; 24e5dd7070Spatrick class LangOptions; 25e5dd7070Spatrick class FunctionDecl; 26e5dd7070Spatrick class VarDecl; 27e5dd7070Spatrick 28e5dd7070Spatrick namespace interp { 29e5dd7070Spatrick class Function; 30e5dd7070Spatrick class Program; 31e5dd7070Spatrick class State; 32e5dd7070Spatrick enum PrimType : unsigned; 33e5dd7070Spatrick 34e5dd7070Spatrick /// Holds all information required to evaluate constexpr code in a module. 35*12c85518Srobert class Context final { 36e5dd7070Spatrick public: 37e5dd7070Spatrick /// Initialises the constexpr VM. 38e5dd7070Spatrick Context(ASTContext &Ctx); 39e5dd7070Spatrick 40e5dd7070Spatrick /// Cleans up the constexpr VM. 41e5dd7070Spatrick ~Context(); 42e5dd7070Spatrick 43e5dd7070Spatrick /// Checks if a function is a potential constant expression. 44e5dd7070Spatrick bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl); 45e5dd7070Spatrick 46e5dd7070Spatrick /// Evaluates a toplevel expression as an rvalue. 47e5dd7070Spatrick bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result); 48e5dd7070Spatrick 49e5dd7070Spatrick /// Evaluates a toplevel initializer. 50e5dd7070Spatrick bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result); 51e5dd7070Spatrick 52e5dd7070Spatrick /// Returns the AST context. getASTContext()53e5dd7070Spatrick ASTContext &getASTContext() const { return Ctx; } 54e5dd7070Spatrick /// Returns the language options. 55e5dd7070Spatrick const LangOptions &getLangOpts() const; 56e5dd7070Spatrick /// Returns the interpreter stack. getStack()57e5dd7070Spatrick InterpStack &getStack() { return Stk; } 58e5dd7070Spatrick /// Returns CHAR_BIT. 59e5dd7070Spatrick unsigned getCharBit() const; 60e5dd7070Spatrick 61e5dd7070Spatrick /// Classifies an expression. 62*12c85518Srobert std::optional<PrimType> classify(QualType T) const; 63e5dd7070Spatrick 64e5dd7070Spatrick private: 65e5dd7070Spatrick /// Runs a function. 66e5dd7070Spatrick bool Run(State &Parent, Function *Func, APValue &Result); 67e5dd7070Spatrick 68*12c85518Srobert /// Checks a result from the interpreter. 69e5dd7070Spatrick bool Check(State &Parent, llvm::Expected<bool> &&R); 70e5dd7070Spatrick 71e5dd7070Spatrick /// Current compilation context. 72e5dd7070Spatrick ASTContext &Ctx; 73e5dd7070Spatrick /// Interpreter stack, shared across invocations. 74e5dd7070Spatrick InterpStack Stk; 75e5dd7070Spatrick /// Constexpr program. 76e5dd7070Spatrick std::unique_ptr<Program> P; 77e5dd7070Spatrick }; 78e5dd7070Spatrick 79e5dd7070Spatrick } // namespace interp 80e5dd7070Spatrick } // namespace clang 81e5dd7070Spatrick 82e5dd7070Spatrick #endif 83