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#include "Function.h"
10#include "Program.h"
11#include "clang/AST/ASTLambda.h"
12#include "clang/AST/Decl.h"
13#include "clang/AST/DeclCXX.h"
14
15using namespace clang;
16using namespace clang::interp;
17
18Function::Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
19 llvm::SmallVectorImpl<ParamDescriptor> &&ParamDescriptors,
20 bool HasThisPointer, bool HasRVO, bool IsLambdaStaticInvoker)
21 : P(P), Kind(FunctionKind::Normal), Source(Source), ArgSize(ArgSize),
22 ParamDescriptors(std::move(ParamDescriptors)), IsValid(false),
23 IsFullyCompiled(false), HasThisPointer(HasThisPointer), HasRVO(HasRVO),
24 HasBody(false), Defined(false) {
25 for (ParamDescriptor PD : this->ParamDescriptors) {
26 Params.insert(KV: {PD.Offset, PD});
27 }
28 assert(Params.size() == this->ParamDescriptors.size());
29
30 if (const auto *F = dyn_cast<const FunctionDecl *>(Val&: Source)) {
31 Variadic = F->isVariadic();
32 Immediate = F->isImmediateFunction();
33 Constexpr = F->isConstexpr();
34 if (const auto *CD = dyn_cast<CXXConstructorDecl>(Val: F)) {
35 Virtual = CD->isVirtual();
36 Kind = FunctionKind::Ctor;
37 } else if (const auto *CD = dyn_cast<CXXDestructorDecl>(Val: F)) {
38 Virtual = CD->isVirtual();
39 Kind = FunctionKind::Dtor;
40 } else if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: F)) {
41 Virtual = MD->isVirtual();
42 if (IsLambdaStaticInvoker)
43 Kind = FunctionKind::LambdaStaticInvoker;
44 else if (clang::isLambdaCallOperator(DC: F))
45 Kind = FunctionKind::LambdaCallOperator;
46 else if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())
47 Kind = FunctionKind::CopyOrMoveOperator;
48 } else {
49 Virtual = false;
50 }
51 } else {
52 Variadic = false;
53 Virtual = false;
54 Immediate = false;
55 Constexpr = false;
56 }
57}
58
59Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
60 auto It = Params.find(Val: Offset);
61 assert(It != Params.end() && "Invalid parameter offset");
62 return It->second;
63}
64
65SourceInfo Function::getSource(CodePtr PC) const {
66 assert(PC >= getCodeBegin() && "PC does not belong to this function");
67 assert(PC <= getCodeEnd() && "PC Does not belong to this function");
68 assert(hasBody() && "Function has no body");
69 unsigned Offset = PC - getCodeBegin();
70 using Elem = std::pair<unsigned, SourceInfo>;
71 auto It = llvm::lower_bound(Range: SrcMap, Value: Elem{Offset, {}}, C: llvm::less_first());
72 if (It == SrcMap.end())
73 return SrcMap.back().second;
74 return It->second;
75}
76