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 /// Evalute \param E and if it can be evaluated to a string literal,
71 /// run strlen() on it.
72 bool evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result);
73
74 /// Returns the AST context.
75 ASTContext &getASTContext() const { return Ctx; }
76 /// Returns the language options.
77 const LangOptions &getLangOpts() const;
78 /// Returns CHAR_BIT.
79 unsigned getCharBit() const;
80 /// Return the floating-point semantics for T.
81 const llvm::fltSemantics &getFloatSemantics(QualType T) const;
82 /// Return the size of T in bits.
83 uint32_t getBitWidth(QualType T) const { return Ctx.getIntWidth(T); }
84
85 /// Classifies a type.
87
88 /// Classifies an expression.
89 OptPrimType classify(const Expr *E) const {
90 assert(E);
91 if (E->isGLValue())
92 return PT_Ptr;
93
94 return classify(E->getType());
95 }
96
98 if (const auto *BT = dyn_cast<BuiltinType>(T)) {
99 if (BT->isInteger() || BT->isFloatingPoint())
100 return true;
101 if (BT->getKind() == BuiltinType::Bool)
102 return true;
103 }
104
105 if (T->isArrayType() || T->isRecordType() || T->isAnyComplexType() ||
106 T->isVectorType())
107 return false;
108 return classify(T) != std::nullopt;
109 }
110 bool canClassify(const Expr *E) {
111 if (E->isGLValue())
112 return true;
113 return canClassify(E->getType());
114 }
115
116 const CXXMethodDecl *
117 getOverridingFunction(const CXXRecordDecl *DynamicDecl,
118 const CXXRecordDecl *StaticDecl,
119 const CXXMethodDecl *InitialFunction) const;
120
121 const Function *getOrCreateFunction(const FunctionDecl *FuncDecl);
122 const Function *getOrCreateObjCBlock(const BlockExpr *E);
123
124 /// Returns whether we should create a global variable for the
125 /// given ValueDecl.
126 static bool shouldBeGloballyIndexed(const ValueDecl *VD) {
127 if (const auto *V = dyn_cast<VarDecl>(VD))
128 return V->hasGlobalStorage() || V->isConstexpr();
129
130 return false;
131 }
132
133 /// Returns the program. This is only needed for unittests.
134 Program &getProgram() const { return *P; }
135
136 unsigned collectBaseOffset(const RecordDecl *BaseDecl,
137 const RecordDecl *DerivedDecl) const;
138
139 const Record *getRecord(const RecordDecl *D) const;
140
141 unsigned getEvalID() const { return EvalID; }
142
143 /// Unevaluated builtins don't get their arguments put on the stack
144 /// automatically. They instead operate on the AST of their Call
145 /// Expression.
146 /// Similar information is available via ASTContext::BuiltinInfo,
147 /// but that is not correct for our use cases.
148 static bool isUnevaluatedBuiltin(unsigned ID);
149
150private:
151 /// Runs a function.
152 bool Run(State &Parent, const Function *Func);
153
154 template <typename ResultT>
155 bool evaluateStringRepr(State &Parent, const Expr *SizeExpr,
156 const Expr *PtrExpr, ResultT &Result);
157
158 /// Current compilation context.
159 ASTContext &Ctx;
160 /// Interpreter stack, shared across invocations.
161 InterpStack Stk;
162 /// Constexpr program.
163 std::unique_ptr<Program> P;
164 /// ID identifying an evaluation.
165 unsigned EvalID = 0;
166 /// Cached widths (in bits) of common types, for a faster classify().
167 unsigned ShortWidth;
168 unsigned IntWidth;
169 unsigned LongWidth;
170 unsigned LongLongWidth;
171};
172
173} // namespace interp
174} // namespace clang
175
176#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:280
OptPrimType classify(const Expr *E) const
Classifies an expression.
Definition Context.h:89
const Function * getOrCreateObjCBlock(const BlockExpr *E)
Definition Context.cpp:541
~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:97
static bool isUnevaluatedBuiltin(unsigned ID)
Unevaluated builtins don't get their arguments put on the stack automatically.
Definition Context.cpp:613
unsigned getCharBit() const
Returns CHAR_BIT.
Definition Context.cpp:393
Program & getProgram() const
Returns the program. This is only needed for unittests.
Definition Context.h:134
bool evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result)
Evalute.
Definition Context.cpp:240
const llvm::fltSemantics & getFloatSemantics(QualType T) const
Return the floating-point semantics for T.
Definition Context.cpp:399
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition Context.h:126
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:578
bool canClassify(const Expr *E)
Definition Context.h:110
const Record * getRecord(const RecordDecl *D) const
Definition Context.cpp:609
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:451
ASTContext & getASTContext() const
Returns the AST context.
Definition Context.h:75
uint32_t getBitWidth(QualType T) const
Return the size of T in bits.
Definition Context.h:83
OptPrimType classify(QualType T) const
Classifies a type.
Definition Context.cpp:314
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:415
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:141
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