Path: blob/main/contrib/llvm-project/clang/lib/AST/ByteCode/Context.h
213799 views
//===--- Context.h - Context for the constexpr VM ---------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Defines the constexpr execution context.9//10// The execution context manages cached bytecode and the global context.11// It invokes the compiler and interpreter, propagating errors.12//13//===----------------------------------------------------------------------===//1415#ifndef LLVM_CLANG_AST_INTERP_CONTEXT_H16#define LLVM_CLANG_AST_INTERP_CONTEXT_H1718#include "InterpStack.h"1920namespace clang {21class ASTContext;22class LangOptions;23class FunctionDecl;24class VarDecl;25class APValue;26class BlockExpr;2728namespace interp {29class Function;30class Program;31class State;32enum PrimType : unsigned;3334struct ParamOffset {35unsigned Offset;36bool IsPtr;37};3839/// Holds all information required to evaluate constexpr code in a module.40class Context final {41public:42/// Initialises the constexpr VM.43Context(ASTContext &Ctx);4445/// Cleans up the constexpr VM.46~Context();4748/// Checks if a function is a potential constant expression.49bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl);5051/// Evaluates a toplevel expression as an rvalue.52bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);5354/// Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.55bool evaluate(State &Parent, const Expr *E, APValue &Result,56ConstantExprKind Kind);5758/// Evaluates a toplevel initializer.59bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result);6061bool evaluateCharRange(State &Parent, const Expr *SizeExpr,62const Expr *PtrExpr, APValue &Result);63bool evaluateCharRange(State &Parent, const Expr *SizeExpr,64const Expr *PtrExpr, std::string &Result);6566/// Returns the AST context.67ASTContext &getASTContext() const { return Ctx; }68/// Returns the language options.69const LangOptions &getLangOpts() const;70/// Returns CHAR_BIT.71unsigned getCharBit() const;72/// Return the floating-point semantics for T.73const llvm::fltSemantics &getFloatSemantics(QualType T) const;74/// Return the size of T in bits.75uint32_t getBitWidth(QualType T) const { return Ctx.getIntWidth(T); }7677/// Classifies a type.78std::optional<PrimType> classify(QualType T) const;7980/// Classifies an expression.81std::optional<PrimType> classify(const Expr *E) const {82assert(E);83if (E->isGLValue())84return PT_Ptr;8586return classify(E->getType());87}8889const CXXMethodDecl *90getOverridingFunction(const CXXRecordDecl *DynamicDecl,91const CXXRecordDecl *StaticDecl,92const CXXMethodDecl *InitialFunction) const;9394const Function *getOrCreateFunction(const FunctionDecl *FuncDecl);95const Function *getOrCreateObjCBlock(const BlockExpr *E);9697/// Returns whether we should create a global variable for the98/// given ValueDecl.99static bool shouldBeGloballyIndexed(const ValueDecl *VD) {100if (const auto *V = dyn_cast<VarDecl>(VD))101return V->hasGlobalStorage() || V->isConstexpr();102103return false;104}105106/// Returns the program. This is only needed for unittests.107Program &getProgram() const { return *P; }108109unsigned collectBaseOffset(const RecordDecl *BaseDecl,110const RecordDecl *DerivedDecl) const;111112const Record *getRecord(const RecordDecl *D) const;113114unsigned getEvalID() const { return EvalID; }115116/// Unevaluated builtins don't get their arguments put on the stack117/// automatically. They instead operate on the AST of their Call118/// Expression.119/// Similar information is available via ASTContext::BuiltinInfo,120/// but that is not correct for our use cases.121static bool isUnevaluatedBuiltin(unsigned ID);122123private:124/// Runs a function.125bool Run(State &Parent, const Function *Func);126127template <typename ResultT>128bool evaluateStringRepr(State &Parent, const Expr *SizeExpr,129const Expr *PtrExpr, ResultT &Result);130131/// Current compilation context.132ASTContext &Ctx;133/// Interpreter stack, shared across invocations.134InterpStack Stk;135/// Constexpr program.136std::unique_ptr<Program> P;137/// ID identifying an evaluation.138unsigned EvalID = 0;139/// Cached widths (in bits) of common types, for a faster classify().140unsigned ShortWidth;141unsigned IntWidth;142unsigned LongWidth;143unsigned LongLongWidth;144};145146} // namespace interp147} // namespace clang148149#endif150151152