]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bindings/go/llvm/IRBindings.cpp
Vendor import of llvm trunk r351319 (just before the release_80 branch
[FreeBSD/FreeBSD.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 #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"
22
23 using namespace llvm;
24
25 LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef C) {
26   return wrap(ConstantAsMetadata::get(unwrap<Constant>(C)));
27 }
28
29 LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
30   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
31 }
32
33 LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
34                             unsigned Count) {
35   return wrap(
36       MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
37 }
38
39 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
40                                   LLVMMetadataRef Val) {
41   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
42   if (!N)
43     return;
44   if (!Val)
45     return;
46   N->addOperand(unwrap<MDNode>(Val));
47 }
48
49 void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
50   MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
51   unwrap<Instruction>(Inst)->setMetadata(KindID, N);
52 }
53
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));
60 }
61
62 LLVMDebugLocMetadata LLVMGetCurrentDebugLocation2(LLVMBuilderRef Bref) {
63   const auto& Loc = unwrap(Bref)->getCurrentDebugLocation();
64   const auto* InlinedAt = Loc.getInlinedAt();
65   const LLVMDebugLocMetadata md{
66     Loc.getLine(),
67     Loc.getCol(),
68     wrap(Loc.getScope()),
69     InlinedAt == nullptr ? nullptr : wrap(InlinedAt->getRawInlinedAt()),
70   };
71   return md;
72 }
73