fac4126acda34155eeca61ee2012731af8ec041f
[oota-llvm.git] / bindings / go / llvm / IRBindings.cpp
1 //===- IRBindings.cpp - Additional bindings for ir ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines additional C bindings for the ir component.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "IRBindings.h"
15
16 #include "llvm/IR/Attributes.h"
17 #include "llvm/IR/DebugLoc.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Module.h"
22
23 using namespace llvm;
24
25 void LLVMAddFunctionAttr2(LLVMValueRef Fn, uint64_t PA) {
26   Function *Func = unwrap<Function>(Fn);
27   const AttributeSet PAL = Func->getAttributes();
28   AttrBuilder B(PA);
29   const AttributeSet PALnew =
30     PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
31                       AttributeSet::get(Func->getContext(),
32                                         AttributeSet::FunctionIndex, B));
33   Func->setAttributes(PALnew);
34 }
35
36 uint64_t LLVMGetFunctionAttr2(LLVMValueRef Fn) {
37   Function *Func = unwrap<Function>(Fn);
38   const AttributeSet PAL = Func->getAttributes();
39   return PAL.Raw(AttributeSet::FunctionIndex);
40 }
41
42 void LLVMRemoveFunctionAttr2(LLVMValueRef Fn, uint64_t PA) {
43   Function *Func = unwrap<Function>(Fn);
44   const AttributeSet PAL = Func->getAttributes();
45   AttrBuilder B(PA);
46   const AttributeSet PALnew =
47     PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
48                          AttributeSet::get(Func->getContext(),
49                                            AttributeSet::FunctionIndex, B));
50   Func->setAttributes(PALnew);
51 }
52
53 LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef C) {
54   return wrap(ConstantAsMetadata::get(unwrap<Constant>(C)));
55 }
56
57 LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
58   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
59 }
60
61 LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
62                             unsigned Count) {
63   return wrap(
64       MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
65 }
66
67 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs,
68                                     unsigned Count) {
69   return wrap(MDNode::getTemporary(*unwrap(C),
70                                    ArrayRef<Metadata *>(unwrap(MDs), Count)));
71 }
72
73 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
74                                   LLVMMetadataRef Val) {
75   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
76   if (!N)
77     return;
78   if (!Val)
79     return;
80   N->addOperand(unwrap<MDNode>(Val));
81 }
82
83 void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
84   MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
85   unwrap<Instruction>(Inst)->setMetadata(KindID, N);
86 }
87
88 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New) {
89   auto *Node = unwrap<MDNodeFwdDecl>(MD);
90   Node->replaceAllUsesWith(unwrap<MDNode>(New));
91   MDNode::deleteTemporary(Node);
92 }
93
94 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
95                                   unsigned Col, LLVMMetadataRef Scope,
96                                   LLVMMetadataRef InlinedAt) {
97   unwrap(Bref)->SetCurrentDebugLocation(
98       DebugLoc::get(Line, Col, Scope ? unwrap<MDNode>(Scope) : nullptr,
99                     InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
100 }