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
26 if (const auto *F = dyn_cast<const FunctionDecl *>(Val&: Source)) {
27 Variadic = F->isVariadic();
28 Immediate = F->isImmediateFunction();
29 Constexpr = F->isConstexpr();
30 if (const auto *CD = dyn_cast<CXXConstructorDecl>(Val: F)) {
31 Virtual = CD->isVirtual();
32 Kind = FunctionKind::Ctor;
33 } else if (const auto *CD = dyn_cast<CXXDestructorDecl>(Val: F)) {
34 Virtual = CD->isVirtual();
35 Kind = FunctionKind::Dtor;
36 } else if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: F)) {
37 Virtual = MD->isVirtual();
38 if (IsLambdaStaticInvoker)
39 Kind = FunctionKind::LambdaStaticInvoker;
40 else if (clang::isLambdaCallOperator(DC: F))
41 Kind = FunctionKind::LambdaCallOperator;
42 else if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())
43 Kind = FunctionKind::CopyOrMoveOperator;
44 } else {
45 Virtual = false;
46 }
47 } else {
48 Variadic = false;
49 Virtual = false;
50 Immediate = false;
51 Constexpr = false;
52 }
53}
54
55SourceInfo Function::getSource(CodePtr PC) const {
56 assert(PC >= getCodeBegin() && "PC does not belong to this function");
57 assert(PC <= getCodeEnd() && "PC Does not belong to this function");
58 assert(hasBody() && "Function has no body");
59 unsigned Offset = PC - getCodeBegin();
60 using Elem = std::pair<unsigned, SourceInfo>;
61 auto It = llvm::lower_bound(Range: SrcMap, Value: Elem{Offset, {}}, C: llvm::less_first());
62 if (It == SrcMap.end())
63 return SrcMap.back().second;
64 return It->second;
65}
66