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