xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/AST/Interp/Function.cpp (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1*7330f729Sjoerg //===--- Function.h - Bytecode function for the VM --------------*- C++ -*-===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg 
9*7330f729Sjoerg #include "Function.h"
10*7330f729Sjoerg #include "Program.h"
11*7330f729Sjoerg #include "Opcode.h"
12*7330f729Sjoerg #include "clang/AST/Decl.h"
13*7330f729Sjoerg #include "clang/AST/DeclCXX.h"
14*7330f729Sjoerg 
15*7330f729Sjoerg using namespace clang;
16*7330f729Sjoerg using namespace clang::interp;
17*7330f729Sjoerg 
Function(Program & P,const FunctionDecl * F,unsigned ArgSize,llvm::SmallVector<PrimType,8> && ParamTypes,llvm::DenseMap<unsigned,ParamDescriptor> && Params)18*7330f729Sjoerg Function::Function(Program &P, const FunctionDecl *F, unsigned ArgSize,
19*7330f729Sjoerg                    llvm::SmallVector<PrimType, 8> &&ParamTypes,
20*7330f729Sjoerg                    llvm::DenseMap<unsigned, ParamDescriptor> &&Params)
21*7330f729Sjoerg     : P(P), Loc(F->getBeginLoc()), F(F), ArgSize(ArgSize),
22*7330f729Sjoerg       ParamTypes(std::move(ParamTypes)), Params(std::move(Params)) {}
23*7330f729Sjoerg 
getCodeBegin() const24*7330f729Sjoerg CodePtr Function::getCodeBegin() const { return Code.data(); }
25*7330f729Sjoerg 
getCodeEnd() const26*7330f729Sjoerg CodePtr Function::getCodeEnd() const { return Code.data() + Code.size(); }
27*7330f729Sjoerg 
getParamDescriptor(unsigned Offset) const28*7330f729Sjoerg Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
29*7330f729Sjoerg   auto It = Params.find(Offset);
30*7330f729Sjoerg   assert(It != Params.end() && "Invalid parameter offset");
31*7330f729Sjoerg   return It->second;
32*7330f729Sjoerg }
33*7330f729Sjoerg 
getSource(CodePtr PC) const34*7330f729Sjoerg SourceInfo Function::getSource(CodePtr PC) const {
35*7330f729Sjoerg   unsigned Offset = PC - getCodeBegin();
36*7330f729Sjoerg   using Elem = std::pair<unsigned, SourceInfo>;
37*7330f729Sjoerg   auto It = std::lower_bound(SrcMap.begin(), SrcMap.end(), Elem{Offset, {}},
38*7330f729Sjoerg                              [](Elem A, Elem B) { return A.first < B.first; });
39*7330f729Sjoerg   if (It == SrcMap.end() || It->first != Offset)
40*7330f729Sjoerg     llvm::report_fatal_error("missing source location");
41*7330f729Sjoerg   return It->second;
42*7330f729Sjoerg }
43*7330f729Sjoerg 
isVirtual() const44*7330f729Sjoerg bool Function::isVirtual() const {
45*7330f729Sjoerg   if (auto *M = dyn_cast<CXXMethodDecl>(F))
46*7330f729Sjoerg     return M->isVirtual();
47*7330f729Sjoerg   return false;
48*7330f729Sjoerg }
49