clang 21.0.0git
Function.h
Go to the documentation of this file.
1//===--- Function.h - Bytecode function for the 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 Function class which holds all bytecode function-specific data.
10//
11// The scope class which describes local variables is also defined here.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_INTERP_FUNCTION_H
16#define LLVM_CLANG_AST_INTERP_FUNCTION_H
17
18#include "Descriptor.h"
19#include "Source.h"
20#include "clang/AST/ASTLambda.h"
21#include "clang/AST/Attr.h"
22#include "clang/AST/Decl.h"
23#include "llvm/ADT/PointerUnion.h"
24#include "llvm/Support/raw_ostream.h"
25
26namespace clang {
27namespace interp {
28class Program;
29class ByteCodeEmitter;
30class Pointer;
31enum PrimType : uint32_t;
32
33/// Describes a scope block.
34///
35/// The block gathers all the descriptors of the locals defined in this block.
36class Scope final {
37public:
38 /// Information about a local's storage.
39 struct Local {
40 /// Offset of the local in frame.
41 unsigned Offset;
42 /// Descriptor of the local.
44 };
45
47
48 Scope(LocalVectorTy &&Descriptors) : Descriptors(std::move(Descriptors)) {}
49
50 llvm::iterator_range<LocalVectorTy::const_iterator> locals() const {
51 return llvm::make_range(Descriptors.begin(), Descriptors.end());
52 }
53
54private:
55 /// Object descriptors in this block.
56 LocalVectorTy Descriptors;
57};
58
60 llvm::PointerUnion<const FunctionDecl *, const BlockExpr *>;
61
62/// Bytecode function.
63///
64/// Contains links to the bytecode of the function, as well as metadata
65/// describing all arguments and stack-local variables.
66///
67/// # Calling Convention
68///
69/// When calling a function, all argument values must be on the stack.
70///
71/// If the function has a This pointer (i.e. hasThisPointer() returns true,
72/// the argument values need to be preceeded by a Pointer for the This object.
73///
74/// If the function uses Return Value Optimization, the arguments (and
75/// potentially the This pointer) need to be preceeded by a Pointer pointing
76/// to the location to construct the returned value.
77///
78/// After the function has been called, it will remove all arguments,
79/// including RVO and This pointer, from the stack.
80///
81class Function final {
82public:
83 enum class FunctionKind {
84 Normal,
85 Ctor,
86 Dtor,
89 };
90 using ParamDescriptor = std::pair<PrimType, Descriptor *>;
91
92 /// Returns the size of the function's local stack.
93 unsigned getFrameSize() const { return FrameSize; }
94 /// Returns the size of the argument stack.
95 unsigned getArgSize() const { return ArgSize; }
96
97 /// Returns a pointer to the start of the code.
98 CodePtr getCodeBegin() const { return Code.data(); }
99 /// Returns a pointer to the end of the code.
100 CodePtr getCodeEnd() const { return Code.data() + Code.size(); }
101
102 /// Returns the original FunctionDecl.
103 const FunctionDecl *getDecl() const {
104 return dyn_cast<const FunctionDecl *>(Source);
105 }
106 const BlockExpr *getExpr() const {
107 return dyn_cast<const BlockExpr *>(Source);
108 }
109
110 /// Returns the name of the function decl this code
111 /// was generated for.
112 const std::string getName() const {
113 if (!Source || !getDecl())
114 return "<<expr>>";
115
117 }
118
119 /// Returns a parameter descriptor.
120 ParamDescriptor getParamDescriptor(unsigned Offset) const;
121
122 /// Checks if the first argument is a RVO pointer.
123 bool hasRVO() const { return HasRVO; }
124
125 bool hasNonNullAttr() const { return getDecl()->hasAttr<NonNullAttr>(); }
126
127 /// Range over the scope blocks.
128 llvm::iterator_range<llvm::SmallVector<Scope, 2>::const_iterator>
129 scopes() const {
130 return llvm::make_range(Scopes.begin(), Scopes.end());
131 }
132
133 /// Range over argument types.
136 llvm::iterator_range<arg_reverse_iterator> args_reverse() const {
137 return llvm::reverse(ParamTypes);
138 }
139
140 /// Returns a specific scope.
141 Scope &getScope(unsigned Idx) { return Scopes[Idx]; }
142 const Scope &getScope(unsigned Idx) const { return Scopes[Idx]; }
143
144 /// Returns the source information at a given PC.
145 SourceInfo getSource(CodePtr PC) const;
146
147 /// Checks if the function is valid to call in constexpr.
148 bool isConstexpr() const { return IsValid || isLambdaStaticInvoker(); }
149
150 /// Checks if the function is virtual.
151 bool isVirtual() const { return Virtual; };
152
153 /// Checks if the function is a constructor.
154 bool isConstructor() const { return Kind == FunctionKind::Ctor; }
155 /// Checks if the function is a destructor.
156 bool isDestructor() const { return Kind == FunctionKind::Dtor; }
157
158 /// Returns whether this function is a lambda static invoker,
159 /// which we generate custom byte code for.
162 }
163
164 /// Returns whether this function is the call operator
165 /// of a lambda record decl.
166 bool isLambdaCallOperator() const {
168 }
169
170 /// Returns the parent record decl, if any.
172 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(
173 dyn_cast<const FunctionDecl *>(Source)))
174 return MD->getParent();
175 return nullptr;
176 }
177
178 /// Checks if the function is fully done compiling.
179 bool isFullyCompiled() const { return IsFullyCompiled; }
180
181 bool hasThisPointer() const { return HasThisPointer; }
182
183 /// Checks if the function already has a body attached.
184 bool hasBody() const { return HasBody; }
185
186 /// Checks if the function is defined.
187 bool isDefined() const { return Defined; }
188
189 bool isVariadic() const { return Variadic; }
190
191 unsigned getBuiltinID() const { return BuiltinID; }
192
193 bool isBuiltin() const { return getBuiltinID() != 0; }
194
195 bool isUnevaluatedBuiltin() const;
196
197 unsigned getNumParams() const { return ParamTypes.size(); }
198
199 /// Returns the number of parameter this function takes when it's called,
200 /// i.e excluding the instance pointer and the RVO pointer.
201 unsigned getNumWrittenParams() const {
202 assert(getNumParams() >= (unsigned)(hasThisPointer() + hasRVO()));
203 return getNumParams() - hasThisPointer() - hasRVO();
204 }
205 unsigned getWrittenArgSize() const {
206 return ArgSize - (align(primSize(PT_Ptr)) * (hasThisPointer() + hasRVO()));
207 }
208
210 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(
211 dyn_cast<const FunctionDecl *>(Source)))
212 return MD->isExplicitObjectMemberFunction();
213 return false;
214 }
215
216 unsigned getParamOffset(unsigned ParamIndex) const {
217 return ParamOffsets[ParamIndex];
218 }
219
220 PrimType getParamType(unsigned ParamIndex) const {
221 return ParamTypes[ParamIndex];
222 }
223
224private:
225 /// Construct a function representing an actual function.
226 Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
228 llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
229 llvm::SmallVectorImpl<unsigned> &&ParamOffsets, bool HasThisPointer,
230 bool HasRVO);
231
232 /// Sets the code of a function.
233 void setCode(unsigned NewFrameSize, std::vector<std::byte> &&NewCode,
234 SourceMap &&NewSrcMap, llvm::SmallVector<Scope, 2> &&NewScopes,
235 bool NewHasBody) {
236 FrameSize = NewFrameSize;
237 Code = std::move(NewCode);
238 SrcMap = std::move(NewSrcMap);
239 Scopes = std::move(NewScopes);
240 IsValid = true;
241 HasBody = NewHasBody;
242 }
243
244 void setIsFullyCompiled(bool FC) { IsFullyCompiled = FC; }
245 void setDefined(bool D) { Defined = D; }
246
247private:
248 friend class Program;
249 friend class ByteCodeEmitter;
250
251 /// Program reference.
252 Program &P;
253 /// Function Kind.
255 /// Declaration this function was compiled from.
256 FunctionDeclTy Source;
257 /// Local area size: storage + metadata.
258 unsigned FrameSize = 0;
259 /// Size of the argument stack.
260 unsigned ArgSize;
261 /// Program code.
262 std::vector<std::byte> Code;
263 /// Opcode-to-expression mapping.
264 SourceMap SrcMap;
265 /// List of block descriptors.
267 /// List of argument types.
269 /// Map from byte offset to parameter descriptor.
270 llvm::DenseMap<unsigned, ParamDescriptor> Params;
271 /// List of parameter offsets.
273 /// Flag to indicate if the function is valid.
274 bool IsValid = false;
275 /// Flag to indicate if the function is done being
276 /// compiled to bytecode.
277 bool IsFullyCompiled = false;
278 /// Flag indicating if this function takes the this pointer
279 /// as the first implicit argument
280 bool HasThisPointer = false;
281 /// Whether this function has Return Value Optimization, i.e.
282 /// the return value is constructed in the caller's stack frame.
283 /// This is done for functions that return non-primive values.
284 bool HasRVO = false;
285 /// If we've already compiled the function's body.
286 bool HasBody = false;
287 bool Defined = false;
288 bool Variadic = false;
289 bool Virtual = false;
290 unsigned BuiltinID = 0;
291
292public:
293 /// Dumps the disassembled bytecode to \c llvm::errs().
294 void dump() const;
295 void dump(llvm::raw_ostream &OS) const;
296};
297
298} // namespace interp
299} // namespace clang
300
301#endif
This file provides some common utility functions for processing Lambda related AST Constructs.
StringRef P
enum clang::sema::@1704::IndirectLocalPathEntry::EntryKind Kind
const Decl * D
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6414
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2107
bool hasAttr() const
Definition: DeclBase.h:580
Represents a function declaration or definition.
Definition: Decl.h:1935
std::string getQualifiedNameAsString() const
Definition: Decl.cpp:1668
An emitter which links the program to bytecode for later use.
Pointer into the code segment.
Definition: Source.h:30
Bytecode function.
Definition: Function.h:81
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition: Function.h:141
CodePtr getCodeBegin() const
Returns a pointer to the start of the code.
Definition: Function.h:98
bool isDestructor() const
Checks if the function is a destructor.
Definition: Function.h:156
CodePtr getCodeEnd() const
Returns a pointer to the end of the code.
Definition: Function.h:100
const std::string getName() const
Returns the name of the function decl this code was generated for.
Definition: Function.h:112
bool isVirtual() const
Checks if the function is virtual.
Definition: Function.h:151
unsigned getNumParams() const
Definition: Function.h:197
bool isDefined() const
Checks if the function is defined.
Definition: Function.h:187
bool hasNonNullAttr() const
Definition: Function.h:125
PrimType getParamType(unsigned ParamIndex) const
Definition: Function.h:220
std::pair< PrimType, Descriptor * > ParamDescriptor
Definition: Function.h:90
const CXXRecordDecl * getParentDecl() const
Returns the parent record decl, if any.
Definition: Function.h:171
unsigned getFrameSize() const
Returns the size of the function's local stack.
Definition: Function.h:93
bool isLambdaCallOperator() const
Returns whether this function is the call operator of a lambda record decl.
Definition: Function.h:166
const BlockExpr * getExpr() const
Definition: Function.h:106
bool isBuiltin() const
Definition: Function.h:193
bool isFullyCompiled() const
Checks if the function is fully done compiling.
Definition: Function.h:179
bool isConstructor() const
Checks if the function is a constructor.
Definition: Function.h:154
unsigned getParamOffset(unsigned ParamIndex) const
Definition: Function.h:216
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition: Function.h:103
bool hasBody() const
Checks if the function already has a body attached.
Definition: Function.h:184
bool isUnevaluatedBuiltin() const
Definition: Function.cpp:76
bool hasThisPointer() const
Definition: Function.h:181
unsigned getBuiltinID() const
Definition: Function.h:191
void dump() const
Dumps the disassembled bytecode to llvm::errs().
Definition: Disasm.cpp:71
llvm::iterator_range< arg_reverse_iterator > args_reverse() const
Definition: Function.h:136
bool isConstexpr() const
Checks if the function is valid to call in constexpr.
Definition: Function.h:148
const Scope & getScope(unsigned Idx) const
Definition: Function.h:142
bool isThisPointerExplicit() const
Definition: Function.h:209
unsigned getNumWrittenParams() const
Returns the number of parameter this function takes when it's called, i.e excluding the instance poin...
Definition: Function.h:201
unsigned getWrittenArgSize() const
Definition: Function.h:205
unsigned getArgSize() const
Returns the size of the argument stack.
Definition: Function.h:95
bool isLambdaStaticInvoker() const
Returns whether this function is a lambda static invoker, which we generate custom byte code for.
Definition: Function.h:160
SmallVectorImpl< PrimType >::const_reverse_iterator arg_reverse_iterator
Range over argument types.
Definition: Function.h:135
bool isVariadic() const
Definition: Function.h:189
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition: Function.cpp:52
ParamDescriptor getParamDescriptor(unsigned Offset) const
Returns a parameter descriptor.
Definition: Function.cpp:46
llvm::iterator_range< llvm::SmallVector< Scope, 2 >::const_iterator > scopes() const
Range over the scope blocks.
Definition: Function.h:129
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:123
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Describes a scope block.
Definition: Function.h:36
llvm::SmallVector< Local, 8 > LocalVectorTy
Definition: Function.h:46
llvm::iterator_range< LocalVectorTy::const_iterator > locals() const
Definition: Function.h:50
Scope(LocalVectorTy &&Descriptors)
Definition: Function.h:48
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:77
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition: Source.h:100
llvm::PointerUnion< const FunctionDecl *, const BlockExpr * > FunctionDeclTy
Definition: Function.h:60
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:131
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:34
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:23
The JSON file list parser is used to communicate input to InstallAPI.
unsigned int uint32_t
Describes a memory block created by an allocation site.
Definition: Descriptor.h:116
Information about a local's storage.
Definition: Function.h:39
unsigned Offset
Offset of the local in frame.
Definition: Function.h:41
Descriptor * Desc
Descriptor of the local.
Definition: Function.h:43