Function.cpp 1.7 KB

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