clang 21.0.0git
Function.cpp
Go to the documentation of this file.
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/Decl.h"
12#include "clang/AST/DeclCXX.h"
14
15using namespace clang;
16using namespace clang::interp;
17
18Function::Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
20 llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
22 bool HasThisPointer, bool HasRVO)
23 : P(P), Kind(FunctionKind::Normal), Source(Source), ArgSize(ArgSize),
24 ParamTypes(std::move(ParamTypes)), Params(std::move(Params)),
25 ParamOffsets(std::move(ParamOffsets)), HasThisPointer(HasThisPointer),
26 HasRVO(HasRVO) {
27 if (const auto *F = dyn_cast<const FunctionDecl *>(Source)) {
28 Variadic = F->isVariadic();
29 BuiltinID = F->getBuiltinID();
30 if (const auto *CD = dyn_cast<CXXConstructorDecl>(F)) {
31 Virtual = CD->isVirtual();
32 Kind = FunctionKind::Ctor;
33 } else if (const auto *CD = dyn_cast<CXXDestructorDecl>(F)) {
34 Virtual = CD->isVirtual();
35 Kind = FunctionKind::Dtor;
36 } else if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
37 Virtual = MD->isVirtual();
38 if (MD->isLambdaStaticInvoker())
39 Kind = FunctionKind::LambdaStaticInvoker;
41 Kind = FunctionKind::LambdaCallOperator;
42 }
43 }
44}
45
46Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
47 auto It = Params.find(Offset);
48 assert(It != Params.end() && "Invalid parameter offset");
49 return It->second;
50}
51
52SourceInfo Function::getSource(CodePtr PC) const {
53 assert(PC >= getCodeBegin() && "PC does not belong to this function");
54 assert(PC <= getCodeEnd() && "PC Does not belong to this function");
55 assert(hasBody() && "Function has no body");
56 unsigned Offset = PC - getCodeBegin();
57 using Elem = std::pair<unsigned, SourceInfo>;
58 auto It = llvm::lower_bound(SrcMap, Elem{Offset, {}}, llvm::less_first());
59 if (It == SrcMap.end())
60 return SrcMap.back().second;
61 return It->second;
62}
63
64/// Unevaluated builtins don't get their arguments put on the stack
65/// automatically. They instead operate on the AST of their Call
66/// Expression.
67/// Similar information is available via ASTContext::BuiltinInfo,
68/// but that is not correct for our use cases.
69static bool isUnevaluatedBuiltin(unsigned BuiltinID) {
70 return BuiltinID == Builtin::BI__builtin_classify_type ||
71 BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size ||
72 BuiltinID == Builtin::BI__builtin_constant_p ||
73 BuiltinID == Builtin::BI__noop;
74}
75
76bool Function::isUnevaluatedBuiltin() const {
77 return ::isUnevaluatedBuiltin(BuiltinID);
78}
StringRef P
Defines enum values for all the target-independent builtin functions.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static bool isUnevaluatedBuiltin(unsigned BuiltinID)
Unevaluated builtins don't get their arguments put on the stack automatically.
Definition: Function.cpp:69
Pointer into the code segment.
Definition: Source.h:30
std::pair< PrimType, Descriptor * > ParamDescriptor
Definition: Function.h:90
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:77
llvm::PointerUnion< const FunctionDecl *, const BlockExpr * > FunctionDeclTy
Definition: Function.h:60
The JSON file list parser is used to communicate input to InstallAPI.
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition: ASTLambda.h:28