1950b70dcSNandor Licker //===--- Function.h - Bytecode function for the VM --------------*- C++ -*-===//
2950b70dcSNandor Licker //
3950b70dcSNandor Licker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4950b70dcSNandor Licker // See https://llvm.org/LICENSE.txt for license information.
5950b70dcSNandor Licker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6950b70dcSNandor Licker //
7950b70dcSNandor Licker //===----------------------------------------------------------------------===//
8950b70dcSNandor Licker
9950b70dcSNandor Licker #include "Function.h"
10950b70dcSNandor Licker #include "Program.h"
11950b70dcSNandor Licker #include "Opcode.h"
12950b70dcSNandor Licker #include "clang/AST/Decl.h"
13950b70dcSNandor Licker #include "clang/AST/DeclCXX.h"
14950b70dcSNandor Licker
15950b70dcSNandor Licker using namespace clang;
16950b70dcSNandor Licker using namespace clang::interp;
17950b70dcSNandor Licker
Function(Program & P,const FunctionDecl * F,unsigned ArgSize,llvm::SmallVector<PrimType,8> && ParamTypes,llvm::DenseMap<unsigned,ParamDescriptor> && Params)18950b70dcSNandor Licker Function::Function(Program &P, const FunctionDecl *F, unsigned ArgSize,
19950b70dcSNandor Licker llvm::SmallVector<PrimType, 8> &&ParamTypes,
20950b70dcSNandor Licker llvm::DenseMap<unsigned, ParamDescriptor> &&Params)
21950b70dcSNandor Licker : P(P), Loc(F->getBeginLoc()), F(F), ArgSize(ArgSize),
22950b70dcSNandor Licker ParamTypes(std::move(ParamTypes)), Params(std::move(Params)) {}
23950b70dcSNandor Licker
getCodeBegin() const24950b70dcSNandor Licker CodePtr Function::getCodeBegin() const { return Code.data(); }
25950b70dcSNandor Licker
getCodeEnd() const26950b70dcSNandor Licker CodePtr Function::getCodeEnd() const { return Code.data() + Code.size(); }
27950b70dcSNandor Licker
getParamDescriptor(unsigned Offset) const28950b70dcSNandor Licker Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
29950b70dcSNandor Licker auto It = Params.find(Offset);
30950b70dcSNandor Licker assert(It != Params.end() && "Invalid parameter offset");
31950b70dcSNandor Licker return It->second;
32950b70dcSNandor Licker }
33950b70dcSNandor Licker
getSource(CodePtr PC) const34950b70dcSNandor Licker SourceInfo Function::getSource(CodePtr PC) const {
35950b70dcSNandor Licker unsigned Offset = PC - getCodeBegin();
36950b70dcSNandor Licker using Elem = std::pair<unsigned, SourceInfo>;
37*4969a692SKazu Hirata auto It = llvm::lower_bound(SrcMap, Elem{Offset, {}}, llvm::less_first());
38950b70dcSNandor Licker if (It == SrcMap.end() || It->first != Offset)
39950b70dcSNandor Licker llvm::report_fatal_error("missing source location");
40950b70dcSNandor Licker return It->second;
41950b70dcSNandor Licker }
42950b70dcSNandor Licker
isVirtual() const43950b70dcSNandor Licker bool Function::isVirtual() const {
44950b70dcSNandor Licker if (auto *M = dyn_cast<CXXMethodDecl>(F))
45950b70dcSNandor Licker return M->isVirtual();
46950b70dcSNandor Licker return false;
47950b70dcSNandor Licker }
48