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.
50 bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FD);
51 void isPotentialConstantExprUnevaluated(State &Parent, const Expr *E,
52 const FunctionDecl *FD);
53
54 /// Evaluates a toplevel expression as an rvalue.
55 bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);
56
57 /// Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.
58 bool evaluate(State &Parent, const Expr *E, APValue &Result,
59 ConstantExprKind Kind);
60
61 /// Evaluates a toplevel initializer.
62 bool evaluateAsInitializer(State &Parent, const VarDecl *VD, const Expr *Init,
64
65 bool evaluateCharRange(State &Parent, const Expr *SizeExpr,
66 const Expr *PtrExpr, APValue &Result);
67 bool evaluateCharRange(State &Parent, const Expr *SizeExpr,
68 const Expr *PtrExpr, std::string &Result);
69
70 /// Evaluate \param E and if it can be evaluated to a null-terminated string,
71 /// copy the result into \param Result.
72 bool evaluateString(State &Parent, const Expr *E, std::string &Result);
73
74 /// Evalute \param E and if it can be evaluated to a string literal,
75 /// run strlen() on it.
76 bool evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result);
77
78 /// Returns the AST context.
79 ASTContext &getASTContext() const { return Ctx; }
80 /// Returns the language options.
81 const LangOptions &getLangOpts() const;
82 /// Returns CHAR_BIT.
83 unsigned getCharBit() const;
84 /// Return the floating-point semantics for T.
85 const llvm::fltSemantics &getFloatSemantics(QualType T) const;
86 /// Return the size of T in bits.
87 uint32_t getBitWidth(QualType T) const { return Ctx.getIntWidth(T); }
88
89 /// Classifies a type.
91
92 /// Classifies an expression.
93 OptPrimType classify(const Expr *E) const {
94 assert(E);
95 if (E->isGLValue())
96 return PT_Ptr;
97
98 return classify(E->getType());
99 }
100
101 bool canClassify(QualType T) const {
102 if (const auto *BT = dyn_cast<BuiltinType>(T)) {
103 if (BT->isInteger() || BT->isFloatingPoint())
104 return true;
105 if (BT->getKind() == BuiltinType::Bool)
106 return true;
107 }
108 if (T->isPointerOrReferenceType())
109 return true;
110
111 if (T->isArrayType() || T->isRecordType() || T->isAnyComplexType() ||
112 T->isVectorType())
113 return false;
114 return classify(T) != std::nullopt;
115 }
116 bool canClassify(const Expr *E) const {
117 if (E->isGLValue())
118 return true;
119 return canClassify(E->getType());
120 }
121
122 const CXXMethodDecl *
123 getOverridingFunction(const CXXRecordDecl *DynamicDecl,
124 const CXXRecordDecl *StaticDecl,
125 const CXXMethodDecl *InitialFunction) const;
126
127 const Function *getOrCreateFunction(const FunctionDecl *FuncDecl);
128 const Function *getOrCreateObjCBlock(const BlockExpr *E);
129
130 /// Returns whether we should create a global variable for the
131 /// given ValueDecl.
132 static bool shouldBeGloballyIndexed(const ValueDecl *VD) {
133 if (const auto *V = dyn_cast<VarDecl>(VD))
134 return V->hasGlobalStorage() || V->isConstexpr();
135
136 return false;
137 }
138
139 /// Returns the program. This is only needed for unittests.
140 Program &getProgram() const { return *P; }
141
142 unsigned collectBaseOffset(const RecordDecl *BaseDecl,
143 const RecordDecl *DerivedDecl) const;
144
145 const Record *getRecord(const RecordDecl *D) const;
146
147 unsigned getEvalID() const { return EvalID; }
148
149 /// Unevaluated builtins don't get their arguments put on the stack
150 /// automatically. They instead operate on the AST of their Call
151 /// Expression.
152 /// Similar information is available via ASTContext::BuiltinInfo,
153 /// but that is not correct for our use cases.
154 static bool isUnevaluatedBuiltin(unsigned ID);
155
156private:
157 /// Runs a function.
158 bool Run(State &Parent, const Function *Func);
159
160 template <typename ResultT>
161 bool evaluateStringRepr(State &Parent, const Expr *SizeExpr,
162 const Expr *PtrExpr, ResultT &Result);
163
164 /// Current compilation context.
165 ASTContext &Ctx;
166 /// Interpreter stack, shared across invocations.
167 InterpStack Stk;
168 /// Constexpr program.
169 std::unique_ptr<Program> P;
170 /// ID identifying an evaluation.
171 unsigned EvalID = 0;
172 /// Cached widths (in bits) of common types, for a faster classify().
173 unsigned ShortWidth;
174 unsigned IntWidth;
175 unsigned LongWidth;
176 unsigned LongLongWidth;
177};
178
179} // namespace interp
180} // namespace clang
181
182#endif
Defines the clang::ASTContext interface.
#define V(N, I)
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:220
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition Expr.h:6558
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
bool isGLValue() const
Definition Expr.h:287
QualType getType() const
Definition Expr.h:144
Represents a function declaration or definition.
Definition Decl.h:2000
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
A (possibly-)qualified type.
Definition TypeBase.h:937
Represents a struct/union/class.
Definition Decl.h:4312
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
Represents a variable declaration or definition.
Definition Decl.h:926
const LangOptions & getLangOpts() const
Returns the language options.
Definition Context.cpp:329
OptPrimType classify(const Expr *E) const
Classifies an expression.
Definition Context.h:93
const Function * getOrCreateObjCBlock(const BlockExpr *E)
Definition Context.cpp:601
~Context()
Cleans up the constexpr VM.
Definition Context.cpp:38
Context(ASTContext &Ctx)
Initialises the constexpr VM.
Definition Context.cpp:29
bool evaluateCharRange(State &Parent, const Expr *SizeExpr, const Expr *PtrExpr, APValue &Result)
Definition Context.cpp:227
bool evaluateString(State &Parent, const Expr *E, std::string &Result)
Evaluate.
Definition Context.cpp:243
static bool isUnevaluatedBuiltin(unsigned ID)
Unevaluated builtins don't get their arguments put on the stack automatically.
Definition Context.cpp:678
unsigned getCharBit() const
Returns CHAR_BIT.
Definition Context.cpp:442
bool canClassify(const Expr *E) const
Definition Context.h:116
Program & getProgram() const
Returns the program. This is only needed for unittests.
Definition Context.h:140
bool evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result)
Evalute.
Definition Context.cpp:289
const llvm::fltSemantics & getFloatSemantics(QualType T) const
Return the floating-point semantics for T.
Definition Context.cpp:448
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition Context.h:132
void isPotentialConstantExprUnevaluated(State &Parent, const Expr *E, const FunctionDecl *FD)
Definition Context.cpp:60
unsigned collectBaseOffset(const RecordDecl *BaseDecl, const RecordDecl *DerivedDecl) const
Definition Context.cpp:643
const Record * getRecord(const RecordDecl *D) const
Definition Context.cpp:674
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FD)
Checks if a function is a potential constant expression.
Definition Context.cpp:40
const Function * getOrCreateFunction(const FunctionDecl *FuncDecl)
Definition Context.cpp:500
ASTContext & getASTContext() const
Returns the AST context.
Definition Context.h:79
uint32_t getBitWidth(QualType T) const
Return the size of T in bits.
Definition Context.h:87
OptPrimType classify(QualType T) const
Classifies a type.
Definition Context.cpp:363
bool canClassify(QualType T) const
Definition Context.h:101
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result)
Evaluates a toplevel expression as an rvalue.
Definition Context.cpp:73
const CXXMethodDecl * getOverridingFunction(const CXXRecordDecl *DynamicDecl, const CXXRecordDecl *StaticDecl, const CXXMethodDecl *InitialFunction) const
Definition Context.cpp:464
bool evaluate(State &Parent, const Expr *E, APValue &Result, ConstantExprKind Kind)
Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.
Definition Context.cpp:103
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, const Expr *Init, APValue &Result)
Evaluates a toplevel initializer.
Definition Context.cpp:132
unsigned getEvalID() const
Definition Context.h:147
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:79
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
bool Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2081
The JSON file list parser is used to communicate input to InstallAPI.
Expr::ConstantExprKind ConstantExprKind
Definition Expr.h:1042
@ Result
The result type of a method or function.
Definition TypeBase.h:905
const FunctionProtoType * T