1a7dea167SDimitry Andric //===--- Function.h - Bytecode function for the VM --------------*- C++ -*-===// 2a7dea167SDimitry Andric // 3a7dea167SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4a7dea167SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5a7dea167SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6a7dea167SDimitry Andric // 7a7dea167SDimitry Andric //===----------------------------------------------------------------------===// 8a7dea167SDimitry Andric 9a7dea167SDimitry Andric #include "Function.h" 10a7dea167SDimitry Andric #include "Opcode.h" 115f757f3fSDimitry Andric #include "Program.h" 12a7dea167SDimitry Andric #include "clang/AST/Decl.h" 13a7dea167SDimitry Andric #include "clang/AST/DeclCXX.h" 145f757f3fSDimitry Andric #include "clang/Basic/Builtins.h" 15a7dea167SDimitry Andric 16a7dea167SDimitry Andric using namespace clang; 17a7dea167SDimitry Andric using namespace clang::interp; 18a7dea167SDimitry Andric 19a7dea167SDimitry Andric Function::Function(Program &P, const FunctionDecl *F, unsigned ArgSize, 2006c3fb27SDimitry Andric llvm::SmallVectorImpl<PrimType> &&ParamTypes, 21bdd1243dSDimitry Andric llvm::DenseMap<unsigned, ParamDescriptor> &&Params, 2206c3fb27SDimitry Andric llvm::SmallVectorImpl<unsigned> &&ParamOffsets, 235f757f3fSDimitry Andric bool HasThisPointer, bool HasRVO, bool UnevaluatedBuiltin) 24a7dea167SDimitry Andric : P(P), Loc(F->getBeginLoc()), F(F), ArgSize(ArgSize), 25bdd1243dSDimitry Andric ParamTypes(std::move(ParamTypes)), Params(std::move(Params)), 2606c3fb27SDimitry Andric ParamOffsets(std::move(ParamOffsets)), HasThisPointer(HasThisPointer), 275f757f3fSDimitry Andric HasRVO(HasRVO), Variadic(F->isVariadic()), 285f757f3fSDimitry Andric IsUnevaluatedBuiltin(UnevaluatedBuiltin) {} 29a7dea167SDimitry Andric 30a7dea167SDimitry Andric Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const { 31a7dea167SDimitry Andric auto It = Params.find(Offset); 32a7dea167SDimitry Andric assert(It != Params.end() && "Invalid parameter offset"); 33a7dea167SDimitry Andric return It->second; 34a7dea167SDimitry Andric } 35a7dea167SDimitry Andric 36a7dea167SDimitry Andric SourceInfo Function::getSource(CodePtr PC) const { 37bdd1243dSDimitry Andric assert(PC >= getCodeBegin() && "PC does not belong to this function"); 38bdd1243dSDimitry Andric assert(PC <= getCodeEnd() && "PC Does not belong to this function"); 3906c3fb27SDimitry Andric assert(hasBody() && "Function has no body"); 40a7dea167SDimitry Andric unsigned Offset = PC - getCodeBegin(); 41a7dea167SDimitry Andric using Elem = std::pair<unsigned, SourceInfo>; 4281ad6265SDimitry Andric auto It = llvm::lower_bound(SrcMap, Elem{Offset, {}}, llvm::less_first()); 43*0fca6ea1SDimitry Andric if (It == SrcMap.end()) 44*0fca6ea1SDimitry Andric return SrcMap.back().second; 45a7dea167SDimitry Andric return It->second; 46a7dea167SDimitry Andric } 47a7dea167SDimitry Andric 48a7dea167SDimitry Andric bool Function::isVirtual() const { 495f757f3fSDimitry Andric if (const auto *M = dyn_cast<CXXMethodDecl>(F)) 50a7dea167SDimitry Andric return M->isVirtual(); 51a7dea167SDimitry Andric return false; 52a7dea167SDimitry Andric } 53