Debian Codegraph @

clang/lib/AST/ByteCode/Function.cpp

raw
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 "clang/AST/ASTLambda.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, FunctionDeclTy Source, unsigned ArgSize,
19 llvm::SmallVectorImpl<PrimType> &&ParamTypes,
20 llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
21 llvm::SmallVectorImpl<unsigned> &&ParamOffsets,
22 bool HasThisPointer, bool HasRVO, bool IsLambdaStaticInvoker)
23 : P(P), Kind(FunctionKind::Normal), Source(Source), ArgSize(ArgSize),
24 ParamTypes(std::move(ParamTypes)), Params(std::move(Params)),
25 ParamOffsets(std::move(ParamOffsets)), IsValid(false),
26 IsFullyCompiled(false), HasThisPointer(HasThisPointer), HasRVO(HasRVO),
27 HasBody(false), Defined(false) {
28 if (const auto *F = dyn_cast<const FunctionDecl *>(Source)) {
29 Variadic = F->isVariadic();
30 Immediate = F->isImmediateFunction();
31 Constexpr = F->isConstexpr() || F->hasAttr<MSConstexprAttr>();
32 if (const auto *CD = dyn_cast<CXXConstructorDecl>(F)) {
33 Virtual = CD->isVirtual();
34 Kind = FunctionKind::Ctor;
35 } else if (const auto *CD = dyn_cast<CXXDestructorDecl>(F)) {
36 Virtual = CD->isVirtual();
37 Kind = FunctionKind::Dtor;
38 } else if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
39 Virtual = MD->isVirtual();
40 if (IsLambdaStaticInvoker)
41 Kind = FunctionKind::LambdaStaticInvoker;
42 else if (clang::isLambdaCallOperator(F))
43 Kind = FunctionKind::LambdaCallOperator;
44 else if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())
45 Kind = FunctionKind::CopyOrMoveOperator;
46 } else {
47 Virtual = false;
48 }
49 } else {
50 Variadic = false;
51 Virtual = false;
52 Immediate = false;
53 Constexpr = false;
54 }
55 }
56
57 Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
58 auto It = Params.find(Offset);
59 assert(It != Params.end() && "Invalid parameter offset");
60 return It->second;
61 }
62
63 SourceInfo Function::getSource(CodePtr PC) const {
64 assert(PC >= getCodeBegin() && "PC does not belong to this function");
65 assert(PC <= getCodeEnd() && "PC Does not belong to this function");
66 assert(hasBody() && "Function has no body");
67 unsigned Offset = PC - getCodeBegin();
68 using Elem = std::pair<unsigned, SourceInfo>;
69 auto It = llvm::lower_bound(SrcMap, Elem{Offset, {}}, llvm::less_first());
70 if (It == SrcMap.end())
71 return SrcMap.back().second;
72 return It->second;
73 }