clang  14.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 #include "llvm/ADT/PointerIntPair.h"
22 
23 namespace clang {
24 class ASTContext;
25 class LangOptions;
26 class Stmt;
27 class FunctionDecl;
28 class VarDecl;
29 
30 namespace interp {
31 class Function;
32 class Program;
33 class State;
34 enum PrimType : unsigned;
35 
36 /// Holds all information required to evaluate constexpr code in a module.
37 class Context {
38 public:
39  /// Initialises the constexpr VM.
40  Context(ASTContext &Ctx);
41 
42  /// Cleans up the constexpr VM.
43  ~Context();
44 
45  /// Checks if a function is a potential constant expression.
46  bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl);
47 
48  /// Evaluates a toplevel expression as an rvalue.
49  bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);
50 
51  /// Evaluates a toplevel initializer.
52  bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result);
53 
54  /// Returns the AST context.
55  ASTContext &getASTContext() const { return Ctx; }
56  /// Returns the language options.
57  const LangOptions &getLangOpts() const;
58  /// Returns the interpreter stack.
59  InterpStack &getStack() { return Stk; }
60  /// Returns CHAR_BIT.
61  unsigned getCharBit() const;
62 
63  /// Classifies an expression.
65 
66 private:
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 private:
74  /// Current compilation context.
75  ASTContext &Ctx;
76  /// Interpreter stack, shared across invocations.
77  InterpStack Stk;
78  /// Constexpr program.
79  std::unique_ptr<Program> P;
80 };
81 
82 } // namespace interp
83 } // namespace clang
84 
85 #endif
clang::interp::Context::getStack
InterpStack & getStack()
Returns the interpreter stack.
Definition: Context.h:59
clang::interp::Context::getCharBit
unsigned getCharBit() const
Returns CHAR_BIT.
Definition: Context.cpp:109
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
llvm::Optional< PrimType >
clang::interp::Context
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:37
llvm::Expected
Definition: LLVM.h:41
clang::interp::Context::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options.
Definition: Context.cpp:60
clang::interp::Context::evaluateAsRValue
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result)
Evaluates a toplevel expression as an rvalue.
Definition: Context.cpp:49
clang::interp::State
Interface for the VM to interact with the AST walker's context.
Definition: State.h:55
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
clang::interp::Context::Context
Context(ASTContext &Ctx)
Initialises the constexpr VM.
Definition: Context.cpp:25
APValue.h
clang::interp::PrimType
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:27
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
InterpStack.h
clang::interp::Context::evaluateAsInitializer
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result)
Evaluates a toplevel initializer.
Definition: Context.cpp:54
State
LineState State
Definition: UnwrappedLineFormatter.cpp:986
clang::interp::InterpStack
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:22
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:58
clang
Definition: CalledOnceCheck.h:17
clang::interp::Function
Bytecode function.
Definition: Function.h:59
clang::interp::Context::~Context
~Context()
Cleans up the constexpr VM.
Definition: Context.cpp:27
clang::interp::Context::isPotentialConstantExpr
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl)
Checks if a function is a potential constant expression.
Definition: Context.cpp:29
unsigned
clang::interp::Context::getASTContext
ASTContext & getASTContext() const
Returns the AST context.
Definition: Context.h:55
Parent
NodeId Parent
Definition: ASTDiff.cpp:192
clang::index::SymbolKind::Function
@ Function
clang::APValue
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1856
clang::interp::Context::classify
llvm::Optional< PrimType > classify(QualType T)
Classifies an expression.
Definition: Context.cpp:62