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/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 using ParamDescriptor = std::pair<PrimType, Descriptor *>;
84
85 /// Returns the size of the function's local stack.
86 unsigned getFrameSize() const { return FrameSize; }
87 /// Returns the size of the argument stack.
88 unsigned getArgSize() const { return ArgSize; }
89
90 /// Returns a pointer to the start of the code.
91 CodePtr getCodeBegin() const { return Code.data(); }
92 /// Returns a pointer to the end of the code.
93 CodePtr getCodeEnd() const { return Code.data() + Code.size(); }
94
95 /// Returns the original FunctionDecl.