clang  19.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 
21 namespace clang {
22 class ASTContext;
23 class LangOptions;
24 class FunctionDecl;
25 class VarDecl;
26 class APValue;
27 
28 namespace interp {
29 class Function;
30 class Program;
31 class State;
32 enum PrimType : unsigned;
33 
34 struct ParamOffset {
35  unsigned Offset;
36  bool IsPtr;
37 };
38 
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);
44 
45  /// Cleans up the constexpr VM.
46  ~Context();
47 
48  /// Checks if a function is a potential constant expression.
49  bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl);
50 
51  /// Evaluates a toplevel expression as an rvalue.
52  bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);
53 
54  /// Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.
55  bool evaluate(State &Parent, const Expr *E, APValue &Result);
56 
57  /// Evaluates a toplevel initializer.
58  bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result);
59 
60  /// Returns the AST context.
61  ASTContext &getASTContext() const { return Ctx; }
62  /// Returns the language options.
63  const LangOptions &getLangOpts() const;
64  /// Returns the interpreter stack.
65  InterpStack &getStack() { return Stk; }
66  /// Returns CHAR_BIT.
67  unsigned getCharBit() const;
68  /// Return the floating-point semantics for T.
69  const llvm::fltSemantics &getFloatSemantics(QualType T) const;
70  /// Return the size of T in bits.
71  uint32_t getBitWidth(QualType T) const { return Ctx.getIntWidth(T); }
72 
73  /// Classifies a type.
74  std::optional<PrimType> classify(QualType T) const;
75 
76  /// Classifies an expression.
77  std::optional<PrimType> classify(const Expr *E) const {
78  assert(E);
79  if (E->isGLValue()) {
80  if (E->getType()->isFunctionType())
81  return PT_FnPtr;
82  return PT_Ptr;
83  }
84 
85  return classify(E->getType());
86  }
87 
88  const CXXMethodDecl *
89  getOverridingFunction(const CXXRecordDecl *DynamicDecl,
90  const CXXRecordDecl *StaticDecl,
91  const CXXMethodDecl *InitialFunction) const;
92 
93  const Function *getOrCreateFunction(const FunctionDecl *FD);
94 
95  /// Returns whether we should create a global variable for the
96  /// given ValueDecl.
97  static bool shouldBeGloballyIndexed(const ValueDecl *VD) {
98  if (const auto *V = dyn_cast<VarDecl>(VD))
99  return V->hasGlobalStorage() || V->isConstexpr();
100 
101  return false;
102  }
103 
104  /// Returns the program. This is only needed for unittests.
105  Program &getProgram() const { return *P.get(); }
106 
107  unsigned collectBaseOffset(const RecordDecl *BaseDecl,
108  const RecordDecl *DerivedDecl) const;
109 
110 private:
111  /// Runs a function.
112  bool Run(State &Parent, const Function *Func, APValue &Result);
113 
114  /// Current compilation context.
115  ASTContext &Ctx;
116  /// Interpreter stack, shared across invocations.
117  InterpStack Stk;
118  /// Constexpr program.
119  std::unique_ptr<Program> P;
120 };
121 
122 } // namespace interp
123 } // namespace clang
124 
125 #endif
#define V(N, I)
Definition: ASTContext.h:3299
NodeId Parent
Definition: ASTDiff.cpp:191
LineState State
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:185
unsigned getIntWidth(QualType T) const
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2060
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
This represents one expression.
Definition: Expr.h:110
bool isGLValue() const
Definition: Expr.h:280
QualType getType() const
Definition: Expr.h:142
Represents a function declaration or definition.
Definition: Decl.h:1972
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:482
A (possibly-)qualified type.
Definition: Type.h:940
Represents a struct/union/class.
Definition: Decl.h:4171
bool isFunctionType() const
Definition: Type.h:7620
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:707
Represents a variable declaration or definition.
Definition: Decl.h:919
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
const LangOptions & getLangOpts() const
Returns the language options.
Definition: Context.cpp:117
~Context()
Cleans up the constexpr VM.
Definition: Context.cpp:27
ASTContext & getASTContext() const
Returns the AST context.
Definition: Context.h:61
Context(ASTContext &Ctx)
Initialises the constexpr VM.
Definition: Context.cpp:25
std::optional< PrimType > classify(const Expr *E) const
Classifies an expression.
Definition: Context.h:77
InterpStack & getStack()
Returns the interpreter stack.
Definition: Context.h:65
bool evaluate(State &Parent, const Expr *E, APValue &Result)
Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.
Definition: Context.cpp:67
unsigned getCharBit() const
Returns CHAR_BIT.
Definition: Context.cpp:183
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result)
Evaluates a toplevel initializer.
Definition: Context.cpp:90
const llvm::fltSemantics & getFloatSemantics(QualType T) const
Return the floating-point semantics for T.
Definition: Context.cpp:189
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition: Context.h:97
unsigned collectBaseOffset(const RecordDecl *BaseDecl, const RecordDecl *DerivedDecl) const
Definition: Context.cpp:267
uint32_t getBitWidth(QualType T) const
Return the size of T in bits.
Definition: Context.h:71
Program & getProgram() const
Returns the program. This is only needed for unittests.
Definition: Context.h:105
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:42
const CXXMethodDecl * getOverridingFunction(const CXXRecordDecl *DynamicDecl, const CXXRecordDecl *StaticDecl, const CXXMethodDecl *InitialFunction) const
Definition: Context.cpp:214
std::optional< PrimType > classify(QualType T) const
Classifies a type.
Definition: Context.cpp:119
const Function * getOrCreateFunction(const FunctionDecl *FD)
Definition: Context.cpp:250
Bytecode function.
Definition: Function.h:77
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:26
The program contains and links the bytecode for all functions.
Definition: Program.h:39
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:32
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T