xref: /freebsd/contrib/llvm-project/clang/lib/AST/ByteCode/Function.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //===--- Function.h - Bytecode function for the VM --------------*- C++ -*-===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric 
9*700637cbSDimitry Andric #include "Function.h"
10*700637cbSDimitry Andric #include "Program.h"
11*700637cbSDimitry Andric #include "clang/AST/Decl.h"
12*700637cbSDimitry Andric #include "clang/AST/DeclCXX.h"
13*700637cbSDimitry Andric 
14*700637cbSDimitry Andric using namespace clang;
15*700637cbSDimitry Andric using namespace clang::interp;
16*700637cbSDimitry Andric 
Function(Program & P,FunctionDeclTy Source,unsigned ArgSize,llvm::SmallVectorImpl<PrimType> && ParamTypes,llvm::DenseMap<unsigned,ParamDescriptor> && Params,llvm::SmallVectorImpl<unsigned> && ParamOffsets,bool HasThisPointer,bool HasRVO,bool IsLambdaStaticInvoker)17*700637cbSDimitry Andric Function::Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
18*700637cbSDimitry Andric                    llvm::SmallVectorImpl<PrimType> &&ParamTypes,
19*700637cbSDimitry Andric                    llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
20*700637cbSDimitry Andric                    llvm::SmallVectorImpl<unsigned> &&ParamOffsets,
21*700637cbSDimitry Andric                    bool HasThisPointer, bool HasRVO, bool IsLambdaStaticInvoker)
22*700637cbSDimitry Andric     : P(P), Kind(FunctionKind::Normal), Source(Source), ArgSize(ArgSize),
23*700637cbSDimitry Andric       ParamTypes(std::move(ParamTypes)), Params(std::move(Params)),
24*700637cbSDimitry Andric       ParamOffsets(std::move(ParamOffsets)), IsValid(false),
25*700637cbSDimitry Andric       IsFullyCompiled(false), HasThisPointer(HasThisPointer), HasRVO(HasRVO),
26*700637cbSDimitry Andric       HasBody(false), Defined(false) {
27*700637cbSDimitry Andric   if (const auto *F = dyn_cast<const FunctionDecl *>(Source)) {
28*700637cbSDimitry Andric     Variadic = F->isVariadic();
29*700637cbSDimitry Andric     Immediate = F->isImmediateFunction();
30*700637cbSDimitry Andric     Constexpr = F->isConstexpr() || F->hasAttr<MSConstexprAttr>();
31*700637cbSDimitry Andric     if (const auto *CD = dyn_cast<CXXConstructorDecl>(F)) {
32*700637cbSDimitry Andric       Virtual = CD->isVirtual();
33*700637cbSDimitry Andric       Kind = FunctionKind::Ctor;
34*700637cbSDimitry Andric     } else if (const auto *CD = dyn_cast<CXXDestructorDecl>(F)) {
35*700637cbSDimitry Andric       Virtual = CD->isVirtual();
36*700637cbSDimitry Andric       Kind = FunctionKind::Dtor;
37*700637cbSDimitry Andric     } else if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
38*700637cbSDimitry Andric       Virtual = MD->isVirtual();
39*700637cbSDimitry Andric       if (IsLambdaStaticInvoker)
40*700637cbSDimitry Andric         Kind = FunctionKind::LambdaStaticInvoker;
41*700637cbSDimitry Andric       else if (clang::isLambdaCallOperator(F))
42*700637cbSDimitry Andric         Kind = FunctionKind::LambdaCallOperator;
43*700637cbSDimitry Andric       else if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())
44*700637cbSDimitry Andric         Kind = FunctionKind::CopyOrMoveOperator;
45*700637cbSDimitry Andric     } else {
46*700637cbSDimitry Andric       Virtual = false;
47*700637cbSDimitry Andric     }
48*700637cbSDimitry Andric   } else {
49*700637cbSDimitry Andric     Variadic = false;
50*700637cbSDimitry Andric     Virtual = false;
51*700637cbSDimitry Andric     Immediate = false;
52*700637cbSDimitry Andric     Constexpr = false;
53*700637cbSDimitry Andric   }
54*700637cbSDimitry Andric }
55*700637cbSDimitry Andric 
getParamDescriptor(unsigned Offset) const56*700637cbSDimitry Andric Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
57*700637cbSDimitry Andric   auto It = Params.find(Offset);
58*700637cbSDimitry Andric   assert(It != Params.end() && "Invalid parameter offset");
59*700637cbSDimitry Andric   return It->second;
60*700637cbSDimitry Andric }
61*700637cbSDimitry Andric 
getSource(CodePtr PC) const62*700637cbSDimitry Andric SourceInfo Function::getSource(CodePtr PC) const {
63*700637cbSDimitry Andric   assert(PC >= getCodeBegin() && "PC does not belong to this function");
64*700637cbSDimitry Andric   assert(PC <= getCodeEnd() && "PC Does not belong to this function");
65*700637cbSDimitry Andric   assert(hasBody() && "Function has no body");
66*700637cbSDimitry Andric   unsigned Offset = PC - getCodeBegin();
67*700637cbSDimitry Andric   using Elem = std::pair<unsigned, SourceInfo>;
68*700637cbSDimitry Andric   auto It = llvm::lower_bound(SrcMap, Elem{Offset, {}}, llvm::less_first());
69*700637cbSDimitry Andric   if (It == SrcMap.end())
70*700637cbSDimitry Andric     return SrcMap.back().second;
71*700637cbSDimitry Andric   return It->second;
72*700637cbSDimitry Andric }
73