1 //===- IRBindings.cpp - Additional bindings for ir ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Module.h"
24 LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef C) {
25 return wrap(ConstantAsMetadata::get(unwrap<Constant>(C)));
28 LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
29 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
32 LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
35 MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
38 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs,
40 return wrap(MDTuple::getTemporary(*unwrap(C),
41 ArrayRef<Metadata *>(unwrap(MDs), Count))
45 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
46 LLVMMetadataRef Val) {
47 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
52 N->addOperand(unwrap<MDNode>(Val));
55 void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
56 MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
57 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
60 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New) {
61 auto *Node = unwrap<MDNode>(MD);
62 Node->replaceAllUsesWith(unwrap<Metadata>(New));
63 MDNode::deleteTemporary(Node);
66 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
67 unsigned Col, LLVMMetadataRef Scope,
68 LLVMMetadataRef InlinedAt) {
69 unwrap(Bref)->SetCurrentDebugLocation(
70 DebugLoc::get(Line, Col, Scope ? unwrap<MDNode>(Scope) : nullptr,
71 InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
74 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
75 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));