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. 10 // 11 // The execution context manages cached bytecode and the global context. 12 // It invokes the compiler and interpreter, propagating errors. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_CLANG_AST_INTERP_CONTEXT_H 17 #define LLVM_CLANG_AST_INTERP_CONTEXT_H 18 19 #include "InterpStack.h" 20 #include "clang/AST/APValue.h" 21 22 namespace clang { 23 class ASTContext; 24 class LangOptions; 25 class FunctionDecl; 26 class VarDecl; 27 28 namespace interp { 29 class Function; 30 class Program; 31 class State; 32 enum PrimType : unsigned; 33 34 /// Holds all information required to evaluate constexpr code in a module. 35 class Context final { 36 public: 37 /// Initialises the constexpr VM. 38 Context(ASTContext &Ctx); 39 40 /// Cleans up the constexpr VM. 41 ~Context(); 42 43 /// Checks if a function is a potential constant expression. 44 bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl); 45 46 /// Evaluates a toplevel expression as an rvalue. 47 bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result); 48 49 /// Evaluates a toplevel initializer. 50 bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result); 51 52 /// Returns the AST context. 53 ASTContext &getASTContext() const { return Ctx; } 54 /// Returns the language options. 55 const LangOptions &getLangOpts() const; 56 /// Returns the interpreter stack. 57 InterpStack &getStack() { return Stk; } 58 /// Returns CHAR_BIT. 59 unsigned getCharBit() const; 60 /// Return the floating-point semantics for T. 61 const llvm::fltSemantics &getFloatSemantics(QualType T) const; 62 63 /// Classifies an expression. 64 std::optional<PrimType> classify(QualType T) const; 65 66 const CXXMethodDecl * 67 getOverridingFunction(const CXXRecordDecl *DynamicDecl, 68 const CXXRecordDecl *StaticDecl, 69 const CXXMethodDecl *InitialFunction) const; 70 /// Returns whether we should create a global variable for the 71 /// given ValueDecl. 72 static bool shouldBeGloballyIndexed(const ValueDecl *VD) { 73 if (const auto *V = dyn_cast<VarDecl>(VD)) 74 return V->hasGlobalStorage() || V->isConstexpr(); 75 76 return false; 77 } 78 79 private: 80 /// Runs a function. 81 bool Run(State &Parent, const Function *Func, APValue &Result); 82 83 /// Checks a result from the interpreter. 84 bool Check(State &Parent, llvm::Expected<bool> &&R); 85 86 /// Current compilation context. 87 ASTContext &Ctx; 88 /// Interpreter stack, shared across invocations. 89 InterpStack Stk; 90 /// Constexpr program. 91 std::unique_ptr<Program> P; 92 }; 93 94 } // namespace interp 95 } // namespace clang 96 97 #endif 98