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