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   if (!Scope)
57     unwrap(Bref)->SetCurrentDebugLocation(DebugLoc());
58   else
59     unwrap(Bref)->SetCurrentDebugLocation(DILocation::get(
60         unwrap<MDNode>(Scope)->getContext(), Line, Col, unwrap<MDNode>(Scope),
61         InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
62 }
63 
LLVMGoGetCurrentDebugLocation(LLVMBuilderRef Bref)64 LLVMDebugLocMetadata LLVMGoGetCurrentDebugLocation(LLVMBuilderRef Bref) {
65   const auto& Loc = unwrap(Bref)->getCurrentDebugLocation();
66   const auto* InlinedAt = Loc.getInlinedAt();
67   const LLVMDebugLocMetadata md{
68     Loc.getLine(),
69     Loc.getCol(),
70     wrap(Loc.getScope()),
71     InlinedAt == nullptr ? nullptr : wrap(InlinedAt->getRawInlinedAt()),
72   };
73   return md;
74 }
75 
76