xref: /freebsd/contrib/llvm-project/clang/lib/AST/Interp/Function.cpp (revision 1d479bf6b4741fdc24490ad7179bbef0a78af288)
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  
9  #include "Function.h"
10  #include "Program.h"
11  #include "Opcode.h"
12  #include "clang/AST/Decl.h"
13  #include "clang/AST/DeclCXX.h"
14  
15  using namespace clang;
16  using namespace clang::interp;
17  
18  Function::Function(Program &P, const FunctionDecl *F, unsigned ArgSize,
19                     llvm::SmallVectorImpl<PrimType> &&ParamTypes,
20                     llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
21                     llvm::SmallVectorImpl<unsigned> &&ParamOffsets,
22                     bool HasThisPointer, bool HasRVO)
23      : P(P), Loc(F->getBeginLoc()), F(F), ArgSize(ArgSize),
24        ParamTypes(std::move(ParamTypes)), Params(std::move(Params)),
25        ParamOffsets(std::move(ParamOffsets)), HasThisPointer(HasThisPointer),
26        HasRVO(HasRVO) {}
27  
28  Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
29    auto It = Params.find(Offset);
30    assert(It != Params.end() && "Invalid parameter offset");
31    return It->second;
32  }
33  
34  SourceInfo Function::getSource(CodePtr PC) const {
35    assert(PC >= getCodeBegin() && "PC does not belong to this function");
36    assert(PC <= getCodeEnd() && "PC Does not belong to this function");
37    assert(hasBody() && "Function has no body");
38    unsigned Offset = PC - getCodeBegin();
39    using Elem = std::pair<unsigned, SourceInfo>;
40    auto It = llvm::lower_bound(SrcMap, Elem{Offset, {}}, llvm::less_first());
41    assert(It != SrcMap.end());
42    return It->second;
43  }
44  
45  bool Function::isVirtual() const {
46    if (auto *M = dyn_cast<CXXMethodDecl>(F))
47      return M->isVirtual();
48    return false;
49  }
50