clang  16.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 "Pointer.h"
19 #include "Source.h"
20 #include "clang/AST/Decl.h"
21 #include "llvm/Support/raw_ostream.h"
22 
23 namespace clang {
24 namespace interp {
25 class Program;
26 class ByteCodeEmitter;
27 enum PrimType : uint32_t;
28 
29 /// Describes a scope block.
30 ///
31 /// The block gathers all the descriptors of the locals defined in this block.
32 class Scope final {
33 public:
34  /// Information about a local's storage.
35  struct Local {
36  /// Offset of the local in frame.
37  unsigned Offset;
38  /// Descriptor of the local.
40  };
41 
43 
44  Scope(LocalVectorTy &&Descriptors) : Descriptors(std::move(Descriptors)) {}
45 
46  llvm::iterator_range<LocalVectorTy::const_iterator> locals() const {
47  return llvm::make_range(Descriptors.begin(), Descriptors.end());
48  }
49 
50 private:
51  /// Object descriptors in this block.
52  LocalVectorTy Descriptors;
53 };
54 
55 /// Bytecode function.
56 ///
57 /// Contains links to the bytecode of the function, as well as metadata
58 /// describing all arguments and stack-local variables.
59 ///
60 /// # Calling Convention
61 ///
62 /// When calling a function, all argument values must be on the stack.
63 ///
64 /// If the function has a This pointer (i.e. hasThisPointer() returns true,
65 /// the argument values need to be preceeded by a Pointer for the This object.
66 ///
67 /// If the function uses Return Value Optimization, the arguments (and
68 /// potentially the This pointer) need to be proceeded by a Pointer pointing
69 /// to the location to construct the returned value.
70 ///
71 /// After the function has been called, it will remove all arguments,
72 /// including RVO and This pointer, from the stack.
73 ///
74 class Function final {
75 public:
76  using ParamDescriptor = std::pair<PrimType, Descriptor *>;
77 
78  /// Returns the size of the function's local stack.
79  unsigned getFrameSize() const { return FrameSize; }
80  /// Returns the size of the argument stack.
81  unsigned getArgSize() const { return ArgSize; }
82 
83  /// Returns a pointer to the start of the code.
84  CodePtr getCodeBegin() const { return Code.data(); }
85  /// Returns a pointer to the end of the code.
86  CodePtr getCodeEnd() const { return Code.data() + Code.size(); }
87 
88  /// Returns the original FunctionDecl.
89  const FunctionDecl *getDecl() const { return F; }
90 
91  /// Returns the name of the function decl this code
92  /// was generated for.
93  const std::string getName() const { return F->getNameInfo().getAsString(); }
94 
95  /// Returns the location.
96  SourceLocation getLoc() const { return Loc; }
97 
98  /// Returns a parameter descriptor.
100 
101  /// Checks if the first argument is a RVO pointer.
102  bool hasRVO() const { return HasRVO; }
103 
104  /// Range over the scope blocks.
105  llvm::iterator_range<llvm::SmallVector<Scope, 2>::const_iterator>
106  scopes() const {
107  return llvm::make_range(Scopes.begin(), Scopes.end());
108  }
109 
110  /// Range over argument types.
111  using arg_reverse_iterator =
113  llvm::iterator_range<arg_reverse_iterator> args_reverse() const {
114  return llvm::reverse(ParamTypes);
115  }
116 
117  /// Returns a specific scope.
118  Scope &getScope(unsigned Idx) { return Scopes[Idx]; }
119  const Scope &getScope(unsigned Idx) const { return Scopes[Idx]; }
120 
121  /// Returns the source information at a given PC.
122  SourceInfo getSource(CodePtr PC) const;
123 
124  /// Checks if the function is valid to call in constexpr.
125  bool isConstexpr() const { return IsValid; }
126 
127  /// Checks if the function is virtual.
128  bool isVirtual() const;
129 
130  /// Checks if the function is a constructor.
131  bool isConstructor() const { return isa<CXXConstructorDecl>(F); }
132 
133  /// Checks if the function is fully done compiling.
134  bool isFullyCompiled() const { return IsFullyCompiled; }
135 
136  bool hasThisPointer() const { return HasThisPointer; }
137 
138  // Checks if the funtion already has a body attached.
139  bool hasBody() const { return HasBody; }
140 
141  unsigned getNumParams() const { return ParamTypes.size(); }
142 
143 private:
144  /// Construct a function representing an actual function.
145  Function(Program &P, const FunctionDecl *F, unsigned ArgSize,
146  llvm::SmallVector<PrimType, 8> &&ParamTypes,
147  llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
148  bool HasThisPointer, bool HasRVO);
149 
150  /// Sets the code of a function.
151  void setCode(unsigned NewFrameSize, std::vector<char> &&NewCode, SourceMap &&NewSrcMap,
152  llvm::SmallVector<Scope, 2> &&NewScopes) {
153  FrameSize = NewFrameSize;
154  Code = std::move(NewCode);
155  SrcMap = std::move(NewSrcMap);
156  Scopes = std::move(NewScopes);
157  IsValid = true;
158  HasBody = true;
159  }
160 
161  void setIsFullyCompiled(bool FC) { IsFullyCompiled = FC; }
162 
163 private:
164  friend class Program;
165  friend class ByteCodeEmitter;
166 
167  /// Program reference.
168  Program &P;
169  /// Location of the executed code.
170  SourceLocation Loc;
171  /// Declaration this function was compiled from.
172  const FunctionDecl *F;
173  /// Local area size: storage + metadata.
174  unsigned FrameSize = 0;
175  /// Size of the argument stack.
176  unsigned ArgSize;
177  /// Program code.
178  std::vector<char> Code;
179  /// Opcode-to-expression mapping.
180  SourceMap SrcMap;
181  /// List of block descriptors.
183  /// List of argument types.
185  /// Map from byte offset to parameter descriptor.
186  llvm::DenseMap<unsigned, ParamDescriptor> Params;
187  /// Flag to indicate if the function is valid.
188  bool IsValid = false;
189  /// Flag to indicate if the function is done being
190  /// compiled to bytecode.
191  bool IsFullyCompiled = false;
192  /// Flag indicating if this function takes the this pointer
193  /// as the first implicit argument
194  bool HasThisPointer = false;
195  /// Whether this function has Return Value Optimization, i.e.
196  /// the return value is constructed in the caller's stack frame.
197  /// This is done for functions that return non-primive values.
198  bool HasRVO = false;
199  /// If we've already compiled the function's body.
200  bool HasBody = false;
201 
202 public:
203  /// Dumps the disassembled bytecode to \c llvm::errs().
204  void dump() const;
205  void dump(llvm::raw_ostream &OS) const;
206 };
207 
208 } // namespace interp
209 } // namespace clang
210 
211 #endif
Pointer.h
clang::interp::Function::getDecl
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition: Function.h:89
clang::interp::Function::getArgSize
unsigned getArgSize() const
Returns the size of the argument stack.
Definition: Function.h:81
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
llvm::SmallVector< Local, 8 >
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::interp::Scope
Describes a scope block.
Definition: Function.h:32
clang::interp::Function::isConstexpr
bool isConstexpr() const
Checks if the function is valid to call in constexpr.
Definition: Function.h:125
clang::interp::Function::arg_reverse_iterator
SmallVectorImpl< PrimType >::const_reverse_iterator arg_reverse_iterator
Range over argument types.
Definition: Function.h:112
clang::interp::Function::hasRVO
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:102
clang::interp::Function::getSource
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition: Function.cpp:32
Decl.h
Offset
unsigned Offset
Definition: Format.cpp:2717
clang::interp::Scope::Local
Information about a local's storage.
Definition: Function.h:35
Source.h
clang::interp::Function::getCodeEnd
CodePtr getCodeEnd() const
Returns a pointer to the end of the code.
Definition: Function.h:86
clang::interp::Function::getLoc
SourceLocation getLoc() const
Returns the location.
Definition: Function.h:96
clang::interp::Function::args_reverse
llvm::iterator_range< arg_reverse_iterator > args_reverse() const
Definition: Function.h:113
clang::interp::Function::getCodeBegin
CodePtr getCodeBegin() const
Returns a pointer to the start of the code.
Definition: Function.h:84
clang::interp::PrimType
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:28
clang::interp::Function::getNumParams
unsigned getNumParams() const
Definition: Function.h:141
clang::interp::Function::hasBody
bool hasBody() const
Definition: Function.h:139
clang::interp::SourceInfo
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:68
clang::interp::Scope::locals
llvm::iterator_range< LocalVectorTy::const_iterator > locals() const
Definition: Function.h:46
clang::interp::Scope::Local::Offset
unsigned Offset
Offset of the local in frame.
Definition: Function.h:37
clang::interp::ByteCodeEmitter
An emitter which links the program to bytecode for later use.
Definition: ByteCodeEmitter.h:32
clang::interp::Function::scopes
llvm::iterator_range< llvm::SmallVector< Scope, 2 >::const_iterator > scopes() const
Range over the scope blocks.
Definition: Function.h:106
clang::FunctionDecl::getNameInfo
DeclarationNameInfo getNameInfo() const
Definition: Decl.h:2104
P
StringRef P
Definition: ASTMatchersInternal.cpp:564
clang::interp::Function::hasThisPointer
bool hasThisPointer() const
Definition: Function.h:136
clang::interp::Function::getName
const std::string getName() const
Returns the name of the function decl this code was generated for.
Definition: Function.h:93
clang::interp::Function::ParamDescriptor
std::pair< PrimType, Descriptor * > ParamDescriptor
Definition: Function.h:76
clang::interp::SourceMap
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition: Source.h:86
clang::interp::Function::getParamDescriptor
ParamDescriptor getParamDescriptor(unsigned Offset) const
Returns a parameter descriptor.
Definition: Function.cpp:26
clang::interp::CodePtr
Pointer into the code segment.
Definition: Source.h:25
clang::interp::Function::isConstructor
bool isConstructor() const
Checks if the function is a constructor.
Definition: Function.h:131
clang::interp::Function::getScope
const Scope & getScope(unsigned Idx) const
Definition: Function.h:119
std
Definition: Format.h:4477
clang::interp::Descriptor
Describes a memory block created by an allocation site.
Definition: Descriptor.h:51
clang::interp::Function::getScope
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition: Function.h:118
clang::interp::Function::dump
void dump() const
Dumps the disassembled bytecode to llvm::errs().
Definition: Disasm.cpp:33
clang
Definition: CalledOnceCheck.h:17
clang::DeclarationNameInfo::getAsString
std::string getAsString() const
getAsString - Retrieve the human-readable string for this name.
Definition: DeclarationName.cpp:459
clang::interp::Function
Bytecode function.
Definition: Function.h:74
clang::interp::Function::isFullyCompiled
bool isFullyCompiled() const
Checks if the function is fully done compiling.
Definition: Function.h:134
clang::interp::Scope::Scope
Scope(LocalVectorTy &&Descriptors)
Definition: Function.h:44
clang::interp::Scope::Local::Desc
Descriptor * Desc
Descriptor of the local.
Definition: Function.h:39
clang::interp::Function::isVirtual
bool isVirtual() const
Checks if the function is virtual.
Definition: Function.cpp:42
llvm::SmallVectorImpl
Definition: Randstruct.h:18
clang::interp::Program
The program contains and links the bytecode for all functions.
Definition: Program.h:40
clang::interp::Function::getFrameSize
unsigned getFrameSize() const
Returns the size of the function's local stack.
Definition: Function.h:79
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1904
clang::interp::Scope::LocalVectorTy
llvm::SmallVector< Local, 8 > LocalVectorTy
Definition: Function.h:42