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 LLVMMetadataRef
LLVMTemporaryMDNode(LLVMContextRef C
, LLVMMetadataRef
*MDs
,
41 return wrap(MDTuple::getTemporary(*unwrap(C
),
42 ArrayRef
<Metadata
*>(unwrap(MDs
), Count
))
46 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M
, const char *name
,
47 LLVMMetadataRef Val
) {
48 NamedMDNode
*N
= unwrap(M
)->getOrInsertNamedMetadata(name
);
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
{
82 InlinedAt
== nullptr ? nullptr : wrap(InlinedAt
->getRawInlinedAt()),
87 void LLVMSetSubprogram(LLVMValueRef Func
, LLVMMetadataRef SP
) {
88 unwrap
<Function
>(Func
)->setSubprogram(unwrap
<DISubprogram
>(SP
));