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