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/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"
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,
36 MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
39 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
40 LLVMMetadataRef Val) {
41 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
46 N->addOperand(unwrap<MDNode>(Val));
49 void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
50 MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
51 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
54 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
55 unsigned Col, LLVMMetadataRef Scope,
56 LLVMMetadataRef InlinedAt) {
57 unwrap(Bref)->SetCurrentDebugLocation(
58 DebugLoc::get(Line, Col, Scope ? unwrap<MDNode>(Scope) : nullptr,
59 InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
62 LLVMDebugLocMetadata LLVMGetCurrentDebugLocation2(LLVMBuilderRef Bref) {
63 const auto& Loc = unwrap(Bref)->getCurrentDebugLocation();
64 const auto* InlinedAt = Loc.getInlinedAt();
65 const LLVMDebugLocMetadata md{
69 InlinedAt == nullptr ? nullptr : wrap(InlinedAt->getRawInlinedAt()),