Thanks to visit codestin.com
Credit goes to clang.llvm.org

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
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
109 if (T->isArrayType() || T->isRecordType() || T->isAnyComplexType() ||
110 T->isVectorType())
111 return false;
112 return classify(T) != std::nullopt;
113 }
114 bool canClassify(const Expr *E) {
115 if (E->isGLValue())
116 return true;
117 return canClassify(E->getType());
118 }
119
120 const CXXMethodDecl *
121 getOverridingFunction(const CXXRecordDecl *DynamicDecl,
122 const CXXRecordDecl *StaticDecl,
123 const CXXMethodDecl *InitialFunction) const;
124
125 const Function *getOrCreateFunction(const FunctionDecl *FuncDecl);
126 const Function *getOrCreateObjCBlock(const BlockExpr *E);
127
128 /// Returns whether we should create a global variable for the
129 /// given ValueDecl.
130 static bool shouldBeGloballyIndexed(const ValueDecl *VD) {
131 if (const auto *V = dyn_cast<VarDecl>(VD))
132 return V->hasGlobalStorage() || V->isConstexpr();
133
134 return false;
135 }
136
137 /// Returns the program. This is only needed for unittests.
138 Program &getProgram() const { return *P; }
139
140 unsigned collectBaseOffset(const RecordDecl *BaseDecl,
141 const RecordDecl *DerivedDecl) const;
142
143 const Record *getRecord(const RecordDecl *D) const;
144
145 unsigned getEvalID() const { return EvalID; }
146
147 /// Unevaluated builtins don't get their arguments put on the stack
148 /// automatically. They instead operate on the AST of their Call
149 /// Expression.
150 /// Similar information is available via ASTContext::BuiltinInfo,
151 /// but that is not correct for our use cases.
152 static bool isUnevaluatedBuiltin(unsigned ID);
153
154private:
155 /// Runs a function.
156 bool Run(State &Parent, const Function *Func);
157
158 template <typename ResultT>
159 bool evaluateStringRepr(State &Parent, const Expr *SizeExpr,
160 const Expr *PtrExpr, ResultT &Result);
161
162 /// Current compilation context.
163 ASTContext &Ctx;
164 /// Interpreter stack, shared across invocations.
165 InterpStack Stk;
166 /// Constexpr program.
167 std::unique_ptr<Program> P;
168 /// ID identifying an evaluation.
169 unsigned EvalID = 0;
170 /// Cached widths (in bits) of common types, for a faster classify().
171 unsigned ShortWidth;
172 unsigned IntWidth;
173 unsigned LongWidth;
174 unsigned LongLongWidth;
175};
176
177} // namespace interp
178} // namespace clang
179
180#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:188
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:1999
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:4309
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
const LangOptions & getLangOpts() const
Returns the language options.
Definition Context.cpp:326
OptPrimType classify(const Expr *E) const
Classifies an expression.
Definition Context.h:93
const Function * getOrCreateObjCBlock(const BlockExpr *E)
Definition Context.cpp:587
~Context()
Cleans up the constexpr VM.
Definition Context.cpp:35
Context(ASTContext &Ctx)
Initialises the constexpr VM.
Definition Context.cpp:26
bool evaluateCharRange(State &Parent, const Expr *SizeExpr, const Expr *PtrExpr, APValue &Result)
Definition Context.cpp:224
bool canClassify(QualType T)
Definition Context.h:101
bool evaluateString(State &Parent, const Expr *E, std::string &Result)
Evaluate.
Definition Context.cpp:240
static bool isUnevaluatedBuiltin(unsigned ID)
Unevaluated builtins don't get their arguments put on the stack automatically.
Definition Context.cpp:659
unsigned getCharBit() const
Returns CHAR_BIT.
Definition Context.cpp:439
Program & getProgram() const
Returns the program. This is only needed for unittests.
Definition Context.h:138
bool evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result)
Evalute.
Definition Context.cpp:286
const llvm::fltSemantics & getFloatSemantics(QualType T) const
Return the floating-point semantics for T.
Definition Context.cpp:445
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition Context.h:130
void isPotentialConstantExprUnevaluated(State &Parent, const Expr *E, const FunctionDecl *FD)
Definition Context.cpp:57
unsigned collectBaseOffset(const RecordDecl *BaseDecl, const RecordDecl *DerivedDecl) const
Definition Context.cpp:624
bool canClassify(const Expr *E)
Definition Context.h:114
const Record * getRecord(const RecordDecl *D) const
Definition Context.cpp:655
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FD)
Checks if a function is a potential constant expression.
Definition Context.cpp:37
const Function * getOrCreateFunction(const FunctionDecl *FuncDecl)
Definition Context.cpp:497
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:360
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result)
Evaluates a toplevel expression as an rvalue.
Definition Context.cpp:70
const CXXMethodDecl * getOverridingFunction(const CXXRecordDecl *DynamicDecl, const CXXRecordDecl *StaticDecl, const CXXMethodDecl *InitialFunction) const
Definition Context.cpp:461
bool evaluate(State &Parent, const Expr *E, APValue &Result, ConstantExprKind Kind)
Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.
Definition Context.cpp:100
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, const Expr *Init, APValue &Result)
Evaluates a toplevel initializer.
Definition Context.cpp:129
unsigned getEvalID() const
Definition Context.h:145
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:2100
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