xref: /openbsd-src/gnu/llvm/clang/lib/AST/Interp/Function.cpp (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===--- Function.h - Bytecode function for the VM --------------*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick 
9e5dd7070Spatrick #include "Function.h"
10e5dd7070Spatrick #include "Program.h"
11e5dd7070Spatrick #include "Opcode.h"
12e5dd7070Spatrick #include "clang/AST/Decl.h"
13e5dd7070Spatrick #include "clang/AST/DeclCXX.h"
14e5dd7070Spatrick 
15e5dd7070Spatrick using namespace clang;
16e5dd7070Spatrick using namespace clang::interp;
17e5dd7070Spatrick 
Function(Program & P,const FunctionDecl * F,unsigned ArgSize,llvm::SmallVector<PrimType,8> && ParamTypes,llvm::DenseMap<unsigned,ParamDescriptor> && Params,bool HasThisPointer,bool HasRVO)18e5dd7070Spatrick Function::Function(Program &P, const FunctionDecl *F, unsigned ArgSize,
19e5dd7070Spatrick                    llvm::SmallVector<PrimType, 8> &&ParamTypes,
20*12c85518Srobert                    llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
21*12c85518Srobert                    bool HasThisPointer, bool HasRVO)
22e5dd7070Spatrick     : P(P), Loc(F->getBeginLoc()), F(F), ArgSize(ArgSize),
23*12c85518Srobert       ParamTypes(std::move(ParamTypes)), Params(std::move(Params)),
24*12c85518Srobert       HasThisPointer(HasThisPointer), HasRVO(HasRVO) {}
25e5dd7070Spatrick 
getParamDescriptor(unsigned Offset) const26e5dd7070Spatrick Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
27e5dd7070Spatrick   auto It = Params.find(Offset);
28e5dd7070Spatrick   assert(It != Params.end() && "Invalid parameter offset");
29e5dd7070Spatrick   return It->second;
30e5dd7070Spatrick }
31e5dd7070Spatrick 
getSource(CodePtr PC) const32e5dd7070Spatrick SourceInfo Function::getSource(CodePtr PC) const {
33*12c85518Srobert   assert(PC >= getCodeBegin() && "PC does not belong to this function");
34*12c85518Srobert   assert(PC <= getCodeEnd() && "PC Does not belong to this function");
35e5dd7070Spatrick   unsigned Offset = PC - getCodeBegin();
36e5dd7070Spatrick   using Elem = std::pair<unsigned, SourceInfo>;
37*12c85518Srobert   auto It = llvm::lower_bound(SrcMap, Elem{Offset, {}}, llvm::less_first());
38*12c85518Srobert   assert(It != SrcMap.end());
39e5dd7070Spatrick   return It->second;
40e5dd7070Spatrick }
41e5dd7070Spatrick 
isVirtual() const42e5dd7070Spatrick bool Function::isVirtual() const {
43e5dd7070Spatrick   if (auto *M = dyn_cast<CXXMethodDecl>(F))
44e5dd7070Spatrick     return M->isVirtual();
45e5dd7070Spatrick   return false;
46e5dd7070Spatrick }
47