xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/Interp/Context.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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 
21a7dea167SDimitry Andric namespace clang {
22a7dea167SDimitry Andric class ASTContext;
23a7dea167SDimitry Andric class LangOptions;
24a7dea167SDimitry Andric class FunctionDecl;
25a7dea167SDimitry Andric class VarDecl;
26*5f757f3fSDimitry Andric class APValue;
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 
34*5f757f3fSDimitry Andric struct ParamOffset {
35*5f757f3fSDimitry Andric   unsigned Offset;
36*5f757f3fSDimitry Andric   bool IsPtr;
37*5f757f3fSDimitry Andric };
38*5f757f3fSDimitry Andric 
39a7dea167SDimitry Andric /// Holds all information required to evaluate constexpr code in a module.
40bdd1243dSDimitry Andric class Context final {
41a7dea167SDimitry Andric public:
42a7dea167SDimitry Andric   /// Initialises the constexpr VM.
43a7dea167SDimitry Andric   Context(ASTContext &Ctx);
44a7dea167SDimitry Andric 
45a7dea167SDimitry Andric   /// Cleans up the constexpr VM.
46a7dea167SDimitry Andric   ~Context();
47a7dea167SDimitry Andric 
48a7dea167SDimitry Andric   /// Checks if a function is a potential constant expression.
49480093f4SDimitry Andric   bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl);
50a7dea167SDimitry Andric 
51a7dea167SDimitry Andric   /// Evaluates a toplevel expression as an rvalue.
52480093f4SDimitry Andric   bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);
53a7dea167SDimitry Andric 
54a7dea167SDimitry Andric   /// Evaluates a toplevel initializer.
55480093f4SDimitry Andric   bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result);
56a7dea167SDimitry Andric 
57a7dea167SDimitry Andric   /// Returns the AST context.
58a7dea167SDimitry Andric   ASTContext &getASTContext() const { return Ctx; }
59a7dea167SDimitry Andric   /// Returns the language options.
60a7dea167SDimitry Andric   const LangOptions &getLangOpts() const;
61a7dea167SDimitry Andric   /// Returns the interpreter stack.
62a7dea167SDimitry Andric   InterpStack &getStack() { return Stk; }
63a7dea167SDimitry Andric   /// Returns CHAR_BIT.
64a7dea167SDimitry Andric   unsigned getCharBit() const;
6506c3fb27SDimitry Andric   /// Return the floating-point semantics for T.
6606c3fb27SDimitry Andric   const llvm::fltSemantics &getFloatSemantics(QualType T) const;
67*5f757f3fSDimitry Andric   /// Return the size of T in bits.
68*5f757f3fSDimitry Andric   uint32_t getBitWidth(QualType T) const { return Ctx.getIntWidth(T); }
69a7dea167SDimitry Andric 
70a7dea167SDimitry Andric   /// Classifies an expression.
71bdd1243dSDimitry Andric   std::optional<PrimType> classify(QualType T) const;
72a7dea167SDimitry Andric 
7306c3fb27SDimitry Andric   const CXXMethodDecl *
7406c3fb27SDimitry Andric   getOverridingFunction(const CXXRecordDecl *DynamicDecl,
7506c3fb27SDimitry Andric                         const CXXRecordDecl *StaticDecl,
7606c3fb27SDimitry Andric                         const CXXMethodDecl *InitialFunction) const;
77*5f757f3fSDimitry Andric 
78*5f757f3fSDimitry Andric   const Function *getOrCreateFunction(const FunctionDecl *FD);
79*5f757f3fSDimitry Andric 
8006c3fb27SDimitry Andric   /// Returns whether we should create a global variable for the
8106c3fb27SDimitry Andric   /// given ValueDecl.
8206c3fb27SDimitry Andric   static bool shouldBeGloballyIndexed(const ValueDecl *VD) {
8306c3fb27SDimitry Andric     if (const auto *V = dyn_cast<VarDecl>(VD))
8406c3fb27SDimitry Andric       return V->hasGlobalStorage() || V->isConstexpr();
8506c3fb27SDimitry Andric 
8606c3fb27SDimitry Andric     return false;
8706c3fb27SDimitry Andric   }
8806c3fb27SDimitry Andric 
89*5f757f3fSDimitry Andric   /// Returns the program. This is only needed for unittests.
90*5f757f3fSDimitry Andric   Program &getProgram() const { return *P.get(); }
91*5f757f3fSDimitry Andric 
92a7dea167SDimitry Andric private:
93a7dea167SDimitry Andric   /// Runs a function.
9406c3fb27SDimitry Andric   bool Run(State &Parent, const Function *Func, APValue &Result);
95a7dea167SDimitry Andric 
96349cc55cSDimitry Andric   /// Checks a result from the interpreter.
97480093f4SDimitry Andric   bool Check(State &Parent, llvm::Expected<bool> &&R);
98a7dea167SDimitry Andric 
99a7dea167SDimitry Andric   /// Current compilation context.
100a7dea167SDimitry Andric   ASTContext &Ctx;
101a7dea167SDimitry Andric   /// Interpreter stack, shared across invocations.
102a7dea167SDimitry Andric   InterpStack Stk;
103a7dea167SDimitry Andric   /// Constexpr program.
104a7dea167SDimitry Andric   std::unique_ptr<Program> P;
105a7dea167SDimitry Andric };
106a7dea167SDimitry Andric 
107a7dea167SDimitry Andric } // namespace interp
108a7dea167SDimitry Andric } // namespace clang
109a7dea167SDimitry Andric 
110a7dea167SDimitry Andric #endif
111