clang 22.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"
21
22namespace clang {
23class LangOptions;
24class FunctionDecl;
25class VarDecl;
26class APValue;
27class BlockExpr;
28
29namespace interp {
30class Function;
31class Program;
32class State;
33enum PrimType : uint8_t;
34
36 unsigned Offset;
37 bool IsPtr;
38};
39
40/// Holds all information required to evaluate constexpr code in a module.
41class Context final {
42public:
43 /// Initialises the constexpr VM.
44 Context(ASTContext &Ctx);
45
46 /// Cleans up the constexpr VM.
47 ~Context();
48
49 /// Checks if a function is a potential constant expression.
52 const FunctionDecl *FD);
53
54 /// Evaluates a toplevel expression as an rvalue.
56
57 /// Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.
58 bool evaluate(State &Parent, const Expr *E, APValue &Result,
60
61 /// Evaluates a toplevel initializer.
63
64 bool evaluateCharRange(State &Parent, const Expr *SizeExpr,
65 const Expr *PtrExpr, APValue &Result);
66 bool evaluateCharRange(State &Parent, const Expr *SizeExpr,
67 const Expr *PtrExpr, std::string &Result);
68
69 /// Evalute \param E and if it can be evaluated to a string literal,
70 /// run strlen() on it.
71 bool evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result);
72
73 /// Returns the AST context.
74 ASTContext &getASTContext() const { return Ctx; }
75 /// Returns the language options.
76 const LangOptions &getLangOpts() const;
77 /// Returns CHAR_BIT.
78 unsigned getCharBit() const;
79 /// Return the floating-point semantics for T.
80 const llvm::fltSemantics &getFloatSemantics(QualType T) const;
81 /// Return the size of T in bits.
82 uint32_t getBitWidth(QualType T) const { return Ctx.getIntWidth(T); }
83
84 /// Classifies a type.
86
87 /// Classifies an expression.
88 OptPrimType classify(const Expr *E) const {
89 assert(E);
90 if (E->isGLValue())
91 return PT_Ptr;
92
93 return classify(E->getType());
94 }
95
97 if (const auto *BT = dyn_cast<BuiltinType>(T)) {
98 if (BT->isInteger() || BT->isFloatingPoint())
99 return true;
100 if (BT->getKind() == BuiltinType::Bool)
101 return true;
102 }
103
104 if (T->isArrayType() || T->isRecordType() || T->isAnyComplexType() ||
105 T->isVectorType())
106 return false;
107 return classify(T) != std::nullopt;
108 }
109 bool canClassify(const Expr *E) {
110 if (E->isGLValue())
111 return true;
112 return canClassify(E->getType());
113 }
114
115 const CXXMethodDecl *
116 getOverridingFunction(const CXXRecordDecl *DynamicDecl,
117 const CXXRecordDecl *StaticDecl,
118 const CXXMethodDecl *InitialFunction) const;
119
120 const Function *getOrCreateFunction(const FunctionDecl *FuncDecl);
122
123 /// Returns whether we should create a global variable for the
124 /// given ValueDecl.
125 static bool shouldBeGloballyIndexed(const ValueDecl *VD) {
126 if (const auto *V = dyn_cast<VarDecl>(VD))
127 return V->hasGlobalStorage() || V->isConstexpr();
128
129 return false;
130 }
131
132 /// Returns the program. This is only needed for unittests.
133 Program &getProgram() const { return *P; }
134
135 unsigned collectBaseOffset(const RecordDecl *BaseDecl,
136 const RecordDecl *DerivedDecl) const;
137
138 const Record *getRecord(const RecordDecl *D) const;
139
140 unsigned getEvalID() const { return EvalID; }
141
142 /// Unevaluated builtins don't get their arguments put on the stack
143 /// automatically. They instead operate on the AST of their Call
144 /// Expression.
145 /// Similar information is available via ASTContext::BuiltinInfo,
146 /// but that is not correct for our use cases.
147 static bool isUnevaluatedBuiltin(unsigned ID);
148
149private:
150 /// Runs a function.
151 bool Run(State &Parent, const Function *Func);
152
153 template <typename ResultT>
154 bool evaluateStringRepr(State &Parent, const Expr *SizeExpr,
155 const Expr *PtrExpr, ResultT &Result);
156
157 /// Current compilation context.
158 ASTContext &Ctx;
159 /// Interpreter stack, shared across invocations.
160 InterpStack Stk;
161 /// Constexpr program.
162 std::unique_ptr<Program> P;
163 /// ID identifying an evaluation.
164 unsigned EvalID = 0;
165 /// Cached widths (in bits) of common types, for a faster classify().
166 unsigned ShortWidth;
167 unsigned IntWidth;
168 unsigned LongWidth;
169 unsigned LongLongWidth;
170};
171
172} // namespace interp
173} // namespace clang
174
175#endif
Defines the clang::ASTContext interface.
#define V(N, I)
Definition: ASTContext.h:3597
NodeId Parent
Definition: ASTDiff.cpp:191
static char ID
Definition: Arena.cpp:183
const Decl * D
enum clang::sema::@1840::IndirectLocalPathEntry::EntryKind Kind
Expr * E
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:188
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6560
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2129
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
This represents one expression.
Definition: Expr.h:112
ConstantExprKind
Definition: Expr.h:751
Represents a function declaration or definition.
Definition: Decl.h:1999
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:434
A (possibly-)qualified type.
Definition: TypeBase.h:937
Represents a struct/union/class.
Definition: Decl.h:4309
bool isArrayType() const
Definition: TypeBase.h:8679
bool isAnyComplexType() const
Definition: TypeBase.h:8715
bool isVectorType() const
Definition: TypeBase.h:8719
bool isRecordType() const
Definition: TypeBase.h:8707
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:711
Represents a variable declaration or definition.
Definition: Decl.h:925
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:41
const LangOptions & getLangOpts() const
Returns the language options.
Definition: Context.cpp:276
OptPrimType classify(const Expr *E) const
Classifies an expression.
Definition: Context.h:88
const Function * getOrCreateObjCBlock(const BlockExpr *E)
Definition: Context.cpp:556
~Context()
Cleans up the constexpr VM.
Definition: Context.cpp:34
bool evaluateCharRange(State &Parent, const Expr *SizeExpr, const Expr *PtrExpr, APValue &Result)
Definition: Context.cpp:223
bool canClassify(QualType T)
Definition: Context.h:96
static bool isUnevaluatedBuiltin(unsigned ID)
Unevaluated builtins don't get their arguments put on the stack automatically.
Definition: Context.cpp:628
unsigned getCharBit() const
Returns CHAR_BIT.
Definition: Context.cpp:389
Program & getProgram() const
Returns the program. This is only needed for unittests.
Definition: Context.h:133
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result)
Evaluates a toplevel initializer.
Definition: Context.cpp:128
bool evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result)
Evalute.
Definition: Context.cpp:239
const llvm::fltSemantics & getFloatSemantics(QualType T) const
Return the floating-point semantics for T.
Definition: Context.cpp:395
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition: Context.h:125
void isPotentialConstantExprUnevaluated(State &Parent, const Expr *E, const FunctionDecl *FD)
Definition: Context.cpp:56
unsigned collectBaseOffset(const RecordDecl *BaseDecl, const RecordDecl *DerivedDecl) const
Definition: Context.cpp:593
bool canClassify(const Expr *E)
Definition: Context.h:109
const Record * getRecord(const RecordDecl *D) const
Definition: Context.cpp:624
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FD)
Checks if a function is a potential constant expression.
Definition: Context.cpp:36
const Function * getOrCreateFunction(const FunctionDecl *FuncDecl)
Definition: Context.cpp:447
ASTContext & getASTContext() const
Returns the AST context.
Definition: Context.h:74
uint32_t getBitWidth(QualType T) const
Return the size of T in bits.
Definition: Context.h:82
OptPrimType classify(QualType T) const
Classifies a type.
Definition: Context.cpp:310
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result)
Evaluates a toplevel expression as an rvalue.
Definition: Context.cpp:69
const CXXMethodDecl * getOverridingFunction(const CXXRecordDecl *DynamicDecl, const CXXRecordDecl *StaticDecl, const CXXMethodDecl *InitialFunction) const
Definition: Context.cpp:411
bool evaluate(State &Parent, const Expr *E, APValue &Result, ConstantExprKind Kind)
Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.
Definition: Context.cpp:99
unsigned getEvalID() const
Definition: Context.h:140
Bytecode function.
Definition: Function.h:86
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:25
The program contains and links the bytecode for all functions.
Definition: Program.h:36
Structure/Class descriptor.
Definition: Record.h:25
Interface for the VM to interact with the AST walker's context.
Definition: State.h:58
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:34
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
const FunctionProtoType * T