1 //===- IRBindings.cpp - Additional bindings for ir ------------------------===//
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 // This file defines additional C bindings for the ir component.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "IRBindings.h"
14 #include "llvm/IR/Attributes.h"
15 #include "llvm/IR/DebugLoc.h"
16 #include "llvm/IR/DebugInfoMetadata.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Module.h"
21 
22 using namespace llvm;
23 
LLVMConstantAsMetadata(LLVMValueRef C)24 LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef C) {
25   return wrap(ConstantAsMetadata::get(unwrap<Constant>(C)));
26 }
27 
LLVMMDString2(LLVMContextRef C,const char * Str,unsigned SLen)28 LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
29   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
30 }
31 
LLVMMDNode2(LLVMContextRef C,LLVMMetadataRef * MDs,unsigned Count)32 LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
33                             unsigned Count) {
34   return wrap(
35       MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
36 }
37 
LLVMAddNamedMetadataOperand2(LLVMModuleRef M,const char * name,LLVMMetadataRef Val)38 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
39                                   LLVMMetadataRef Val) {
40   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
41   if (!N)
42     return;
43   if (!Val)
44     return;
45   N->addOperand(unwrap<MDNode>(Val));
46 }
47 
LLVMSetMetadata2(LLVMValueRef Inst,unsigned KindID,LLVMMetadataRef MD)48 void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
49   MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
50   unwrap<Instruction>(Inst)->setMetadata(KindID, N);
51 }
52 
LLVMGoSetCurrentDebugLocation(LLVMBuilderRef Bref,unsigned Line,unsigned Col,LLVMMetadataRef Scope,LLVMMetadataRef InlinedAt)53 void LLVMGoSetCurrentDebugLocation(LLVMBuilderRef Bref, unsigned Line,
54                                   unsigned Col, LLVMMetadataRef Scope,
55                                   LLVMMetadataRef InlinedAt) {
56   unwrap(Bref)->SetCurrentDebugLocation(
57       DebugLoc::get(Line, Col, Scope ? unwrap<MDNode>(Scope) : nullptr,
58                     InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
59 }
60 
LLVMGoGetCurrentDebugLocation(LLVMBuilderRef Bref)61 LLVMDebugLocMetadata LLVMGoGetCurrentDebugLocation(LLVMBuilderRef Bref) {
62   const auto& Loc = unwrap(Bref)->getCurrentDebugLocation();
63   const auto* InlinedAt = Loc.getInlinedAt();
64   const LLVMDebugLocMetadata md{
65     Loc.getLine(),
66     Loc.getCol(),
67     wrap(Loc.getScope()),
68     InlinedAt == nullptr ? nullptr : wrap(InlinedAt->getRawInlinedAt()),
69   };
70   return md;
71 }
72 
73