Recommit r310809 with a fix for the spill problem
[llvm-core.git] / bindings / go / llvm / IRBindings.cpp
blob4bfa1bbaf0cc8ac81972947abbc97350e2ec3267
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.
12 //===----------------------------------------------------------------------===//
14 #include "IRBindings.h"
15 #include "llvm/IR/Attributes.h"
16 #include "llvm/IR/DebugLoc.h"
17 #include "llvm/IR/DebugInfoMetadata.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"
23 using namespace llvm;
25 LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef C) {
26 return wrap(ConstantAsMetadata::get(unwrap<Constant>(C)));
29 LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
30 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
33 LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
34 unsigned Count) {
35 return wrap(
36 MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
39 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs,
40 unsigned Count) {
41 return wrap(MDTuple::getTemporary(*unwrap(C),
42 ArrayRef<Metadata *>(unwrap(MDs), Count))
43 .release());
46 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
47 LLVMMetadataRef Val) {
48 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
49 if (!N)
50 return;
51 if (!Val)
52 return;
53 N->addOperand(unwrap<MDNode>(Val));
56 void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
57 MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
58 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
61 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New) {
62 auto *Node = unwrap<MDNode>(MD);
63 Node->replaceAllUsesWith(unwrap<Metadata>(New));
64 MDNode::deleteTemporary(Node);
67 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
68 unsigned Col, LLVMMetadataRef Scope,
69 LLVMMetadataRef InlinedAt) {
70 unwrap(Bref)->SetCurrentDebugLocation(
71 DebugLoc::get(Line, Col, Scope ? unwrap<MDNode>(Scope) : nullptr,
72 InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
75 LLVMDebugLocMetadata LLVMGetCurrentDebugLocation2(LLVMBuilderRef Bref) {
76 const auto& Loc = unwrap(Bref)->getCurrentDebugLocation();
77 const auto* InlinedAt = Loc.getInlinedAt();
78 const LLVMDebugLocMetadata md{
79 Loc.getLine(),
80 Loc.getCol(),
81 wrap(Loc.getScope()),
82 InlinedAt == nullptr ? nullptr : wrap(InlinedAt->getRawInlinedAt()),
84 return md;
87 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
88 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));