xref: /llvm-project/llvm/lib/SandboxIR/Function.cpp (revision e22b07e766e415d6a0ed4c73fe5286fcf25f8d98)
1*e22b07e7Svporpo //===- Function.cpp - The Function class of Sandbox IR --------------------===//
2*e22b07e7Svporpo //
3*e22b07e7Svporpo // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e22b07e7Svporpo // See https://llvm.org/LICENSE.txt for license information.
5*e22b07e7Svporpo // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e22b07e7Svporpo //
7*e22b07e7Svporpo //===----------------------------------------------------------------------===//
8*e22b07e7Svporpo 
9*e22b07e7Svporpo #include "llvm/SandboxIR/Function.h"
10*e22b07e7Svporpo #include "llvm/IR/Value.h"
11*e22b07e7Svporpo #include "llvm/SandboxIR/Context.h"
12*e22b07e7Svporpo 
13*e22b07e7Svporpo namespace llvm::sandboxir {
14*e22b07e7Svporpo 
15*e22b07e7Svporpo FunctionType *Function::getFunctionType() const {
16*e22b07e7Svporpo   return cast<FunctionType>(
17*e22b07e7Svporpo       Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
18*e22b07e7Svporpo }
19*e22b07e7Svporpo 
20*e22b07e7Svporpo #ifndef NDEBUG
21*e22b07e7Svporpo void Function::dumpNameAndArgs(raw_ostream &OS) const {
22*e22b07e7Svporpo   auto *F = cast<llvm::Function>(Val);
23*e22b07e7Svporpo   OS << *F->getReturnType() << " @" << F->getName() << "(";
24*e22b07e7Svporpo   interleave(
25*e22b07e7Svporpo       F->args(),
26*e22b07e7Svporpo       [this, &OS](const llvm::Argument &LLVMArg) {
27*e22b07e7Svporpo         auto *SBArg = cast_or_null<Argument>(Ctx.getValue(&LLVMArg));
28*e22b07e7Svporpo         if (SBArg == nullptr)
29*e22b07e7Svporpo           OS << "NULL";
30*e22b07e7Svporpo         else
31*e22b07e7Svporpo           SBArg->printAsOperand(OS);
32*e22b07e7Svporpo       },
33*e22b07e7Svporpo       [&] { OS << ", "; });
34*e22b07e7Svporpo   OS << ")";
35*e22b07e7Svporpo }
36*e22b07e7Svporpo 
37*e22b07e7Svporpo void Function::dumpOS(raw_ostream &OS) const {
38*e22b07e7Svporpo   dumpNameAndArgs(OS);
39*e22b07e7Svporpo   OS << " {\n";
40*e22b07e7Svporpo   auto *LLVMF = cast<llvm::Function>(Val);
41*e22b07e7Svporpo   interleave(
42*e22b07e7Svporpo       *LLVMF,
43*e22b07e7Svporpo       [this, &OS](const llvm::BasicBlock &LLVMBB) {
44*e22b07e7Svporpo         auto *BB = cast_or_null<BasicBlock>(Ctx.getValue(&LLVMBB));
45*e22b07e7Svporpo         if (BB == nullptr)
46*e22b07e7Svporpo           OS << "NULL";
47*e22b07e7Svporpo         else
48*e22b07e7Svporpo           OS << *BB;
49*e22b07e7Svporpo       },
50*e22b07e7Svporpo       [&OS] { OS << "\n"; });
51*e22b07e7Svporpo   OS << "}\n";
52*e22b07e7Svporpo }
53*e22b07e7Svporpo #endif // NDEBUG
54*e22b07e7Svporpo 
55*e22b07e7Svporpo } // namespace llvm::sandboxir
56