1 //===--- Context.h - Context for the constexpr VM ---------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Defines the constexpr execution context.
11 // The execution context manages cached bytecode and the global context.
12 // It invokes the compiler and interpreter, propagating errors.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_AST_INTERP_CONTEXT_H
17 #define LLVM_CLANG_AST_INTERP_CONTEXT_H
19 #include "InterpStack.h"
32 enum PrimType
: unsigned;
39 /// Holds all information required to evaluate constexpr code in a module.
42 /// Initialises the constexpr VM.
43 Context(ASTContext
&Ctx
);
45 /// Cleans up the constexpr VM.
48 /// Checks if a function is a potential constant expression.
49 bool isPotentialConstantExpr(State
&Parent
, const FunctionDecl
*FnDecl
);
51 /// Evaluates a toplevel expression as an rvalue.
52 bool evaluateAsRValue(State
&Parent
, const Expr
*E
, APValue
&Result
);
54 /// Evaluates a toplevel initializer.
55 bool evaluateAsInitializer(State
&Parent
, const VarDecl
*VD
, APValue
&Result
);
57 /// Returns the AST context.
58 ASTContext
&getASTContext() const { return Ctx
; }
59 /// Returns the language options.
60 const LangOptions
&getLangOpts() const;
61 /// Returns the interpreter stack.
62 InterpStack
&getStack() { return Stk
; }
64 unsigned getCharBit() const;
65 /// Return the floating-point semantics for T.
66 const llvm::fltSemantics
&getFloatSemantics(QualType T
) const;
67 /// Return the size of T in bits.
68 uint32_t getBitWidth(QualType T
) const { return Ctx
.getIntWidth(T
); }
70 /// Classifies an expression.
71 std::optional
<PrimType
> classify(QualType T
) const;
74 getOverridingFunction(const CXXRecordDecl
*DynamicDecl
,
75 const CXXRecordDecl
*StaticDecl
,
76 const CXXMethodDecl
*InitialFunction
) const;
78 const Function
*getOrCreateFunction(const FunctionDecl
*FD
);
80 /// Returns whether we should create a global variable for the
82 static bool shouldBeGloballyIndexed(const ValueDecl
*VD
) {
83 if (const auto *V
= dyn_cast
<VarDecl
>(VD
))
84 return V
->hasGlobalStorage() || V
->isConstexpr();
89 /// Returns the program. This is only needed for unittests.
90 Program
&getProgram() const { return *P
.get(); }
94 bool Run(State
&Parent
, const Function
*Func
, APValue
&Result
);
96 /// Checks a result from the interpreter.
97 bool Check(State
&Parent
, llvm::Expected
<bool> &&R
);
99 /// Current compilation context.
101 /// Interpreter stack, shared across invocations.
103 /// Constexpr program.
104 std::unique_ptr
<Program
> P
;
107 } // namespace interp