[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / AST / Interp / Context.h
blob7649caef2242816a31a4bd4c0aa081da3203d13e
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.
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"
21 namespace clang {
22 class ASTContext;
23 class LangOptions;
24 class FunctionDecl;
25 class VarDecl;
26 class APValue;
28 namespace interp {
29 class Function;
30 class Program;
31 class State;
32 enum PrimType : unsigned;
34 struct ParamOffset {
35 unsigned Offset;
36 bool IsPtr;
39 /// Holds all information required to evaluate constexpr code in a module.
40 class Context final {
41 public:
42 /// Initialises the constexpr VM.
43 Context(ASTContext &Ctx);
45 /// Cleans up the constexpr VM.
46 ~Context();
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; }
63 /// Returns CHAR_BIT.
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;
73 const CXXMethodDecl *
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
81 /// given ValueDecl.
82 static bool shouldBeGloballyIndexed(const ValueDecl *VD) {
83 if (const auto *V = dyn_cast<VarDecl>(VD))
84 return V->hasGlobalStorage() || V->isConstexpr();
86 return false;
89 /// Returns the program. This is only needed for unittests.
90 Program &getProgram() const { return *P.get(); }
92 private:
93 /// Runs a function.
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.
100 ASTContext &Ctx;
101 /// Interpreter stack, shared across invocations.
102 InterpStack Stk;
103 /// Constexpr program.
104 std::unique_ptr<Program> P;
107 } // namespace interp
108 } // namespace clang
110 #endif