clang 17.0.0git
Context.h
Go to the documentation of this file.
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
22namespace clang {
23class ASTContext;
24class LangOptions;
25class FunctionDecl;
26class VarDecl;
27
28namespace interp {
29class Function;
30class Program;
31class State;
32enum PrimType : unsigned;
33
34/// Holds all information required to evaluate constexpr code in a module.
35class Context final {
36public:
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.
45
46 /// Evaluates a toplevel expression as an rvalue.
48
49 /// Evaluates a toplevel initializer.
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
66private:
67 /// Runs a function.
68 bool Run(State &Parent, Function *Func, APValue &Result);
69
70 /// Checks a result from the interpreter.
71 bool Check(State &Parent, llvm::Expected<bool> &&R);
72
73 /// Current compilation context.
74 ASTContext &Ctx;
75 /// Interpreter stack, shared across invocations.
76 InterpStack Stk;
77 /// Constexpr program.
78 std::unique_ptr<Program> P;
79};
80
81} // namespace interp
82} // namespace clang
83
84#endif
NodeId Parent
Definition: ASTDiff.cpp:191
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1917
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:82
A (possibly-)qualified type.
Definition: Type.h:736
Represents a variable declaration or definition.
Definition: Decl.h:913
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:35
const LangOptions & getLangOpts() const
Returns the language options.
Definition: Context.cpp:78
~Context()
Cleans up the constexpr VM.
Definition: Context.cpp:27
unsigned getCharBit() const
Returns CHAR_BIT.
Definition: Context.cpp:132
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result)
Evaluates a toplevel initializer.
Definition: Context.cpp:65
const llvm::fltSemantics & getFloatSemantics(QualType T) const
Return the floating-point semantics for T.
Definition: Context.cpp:138
ASTContext & getASTContext() const
Returns the AST context.
Definition: Context.h:53
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl)
Checks if a function is a potential constant expression.
Definition: Context.cpp:29
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result)
Evaluates a toplevel expression as an rvalue.
Definition: Context.cpp:53
InterpStack & getStack()
Returns the interpreter stack.
Definition: Context.h:57
std::optional< PrimType > classify(QualType T) const
Classifies an expression.
Definition: Context.cpp:80
Bytecode function.
Definition: Function.h:74
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:25
Interface for the VM to interact with the AST walker's context.
Definition: State.h:55
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:30
@ Result
The result type of a method or function.