IR: Split Metadata from Value
[oota-llvm.git] / bindings / go / llvm / DIBuilderBindings.cpp
1 //===- DIBuilderBindings.cpp - Bindings for DIBuilder ---------------------===//
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 C bindings for the DIBuilder class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "DIBuilderBindings.h"
15
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/DIBuilder.h"
18
19 using namespace llvm;
20
21 static Metadata *unwrapMetadata(LLVMValueRef VRef) {
22   Value *V = unwrap(VRef);
23   if (!V)
24     return nullptr;
25   if (auto *MD = dyn_cast<MetadataAsValue>(V))
26     return MD->getMetadata();
27   return ValueAsMetadata::get(V);
28 }
29
30 static SmallVector<Metadata *, 8> unwrapMetadataArray(LLVMValueRef *Data,
31                                                       size_t Length) {
32   SmallVector<Metadata *, 8> Elements;
33   for (size_t I = 0; I != Length; ++I)
34     Elements.push_back(unwrapMetadata(Data[I]));
35   return Elements;
36 }
37
38 namespace {
39 template <typename T> T unwrapDI(LLVMValueRef v) {
40   return T(cast_or_null<MDNode>(unwrapMetadata(v)));
41 }
42 }
43
44 static LLVMValueRef wrapDI(DIDescriptor N) {
45   return wrap(MetadataAsValue::get(N->getContext(), N));
46 }
47
48 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
49
50 LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) {
51   Module *m = unwrap(mref);
52   return wrap(new DIBuilder(*m));
53 }
54
55 void LLVMDIBuilderDestroy(LLVMDIBuilderRef dref) {
56   DIBuilder *d = unwrap(dref);
57   delete d;
58 }
59
60 void LLVMDIBuilderFinalize(LLVMDIBuilderRef dref) { unwrap(dref)->finalize(); }
61
62 LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
63                                             unsigned Lang, const char *File,
64                                             const char *Dir,
65                                             const char *Producer, int Optimized,
66                                             const char *Flags,
67                                             unsigned RuntimeVersion) {
68   DIBuilder *D = unwrap(Dref);
69   DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized,
70                                           Flags, RuntimeVersion);
71   return wrapDI(CU);
72 }
73
74 LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
75                                      const char *Dir) {
76   DIBuilder *D = unwrap(Dref);
77   DIFile F = D->createFile(File, Dir);
78   return wrapDI(F);
79 }
80
81 LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
82                                              LLVMValueRef Scope,
83                                              LLVMValueRef File, unsigned Line,
84                                              unsigned Column) {
85   DIBuilder *D = unwrap(Dref);
86   DILexicalBlock LB = D->createLexicalBlock(
87       unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Line, Column);
88   return wrapDI(LB);
89 }
90
91 LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
92                                                  LLVMValueRef Scope,
93                                                  LLVMValueRef File,
94                                                  unsigned Discriminator) {
95   DIBuilder *D = unwrap(Dref);
96   DILexicalBlockFile LBF = D->createLexicalBlockFile(
97       unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Discriminator);
98   return wrapDI(LBF);
99 }
100
101 LLVMValueRef LLVMDIBuilderCreateFunction(
102     LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
103     const char *LinkageName, LLVMValueRef File, unsigned Line,
104     LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition,
105     unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Func) {
106   DIBuilder *D = unwrap(Dref);
107   DISubprogram SP = D->createFunction(
108       unwrapDI<DIDescriptor>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
109       Line, unwrapDI<DICompositeType>(CompositeType), IsLocalToUnit,
110       IsDefinition, ScopeLine, Flags, IsOptimized, unwrap<Function>(Func));
111   return wrapDI(SP);
112 }
113
114 LLVMValueRef LLVMDIBuilderCreateLocalVariable(
115     LLVMDIBuilderRef Dref, unsigned Tag, LLVMValueRef Scope, const char *Name,
116     LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve,
117     unsigned Flags, unsigned ArgNo) {
118   DIBuilder *D = unwrap(Dref);
119   DIVariable V = D->createLocalVariable(
120       Tag, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
121       unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, ArgNo);
122   return wrapDI(V);
123 }
124
125 LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
126                                           const char *Name, uint64_t SizeInBits,
127                                           uint64_t AlignInBits,
128                                           unsigned Encoding) {
129   DIBuilder *D = unwrap(Dref);
130   DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding);
131   return wrapDI(T);
132 }
133
134 LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
135                                             LLVMValueRef PointeeType,
136                                             uint64_t SizeInBits,
137                                             uint64_t AlignInBits,
138                                             const char *Name) {
139   DIBuilder *D = unwrap(Dref);
140   DIDerivedType T = D->createPointerType(unwrapDI<DIType>(PointeeType),
141                                          SizeInBits, AlignInBits, Name);
142   return wrapDI(T);
143 }
144
145 LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref,
146                                                LLVMValueRef File,
147                                                LLVMValueRef ParameterTypes) {
148   DIBuilder *D = unwrap(Dref);
149   DICompositeType CT = D->createSubroutineType(
150       unwrapDI<DIFile>(File), unwrapDI<DITypeArray>(ParameterTypes));
151   return wrapDI(CT);
152 }
153
154 LLVMValueRef LLVMDIBuilderCreateStructType(
155     LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
156     LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
157     unsigned Flags, LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes) {
158   DIBuilder *D = unwrap(Dref);
159   DICompositeType CT = D->createStructType(
160       unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
161       SizeInBits, AlignInBits, Flags, unwrapDI<DIType>(DerivedFrom),
162       unwrapDI<DIArray>(ElementTypes));
163   return wrapDI(CT);
164 }
165
166 LLVMValueRef LLVMDIBuilderCreateMemberType(
167     LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
168     LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
169     uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty) {
170   DIBuilder *D = unwrap(Dref);
171   DIDerivedType DT = D->createMemberType(
172       unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
173       SizeInBits, AlignInBits, OffsetInBits, Flags, unwrapDI<DIType>(Ty));
174   return wrapDI(DT);
175 }
176
177 LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
178                                           uint64_t SizeInBits,
179                                           uint64_t AlignInBits,
180                                           LLVMValueRef ElementType,
181                                           LLVMValueRef Subscripts) {
182   DIBuilder *D = unwrap(Dref);
183   DICompositeType CT =
184       D->createArrayType(SizeInBits, AlignInBits, unwrapDI<DIType>(ElementType),
185                          unwrapDI<DIArray>(Subscripts));
186   return wrapDI(CT);
187 }
188
189 LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty,
190                                         const char *Name, LLVMValueRef File,
191                                         unsigned Line, LLVMValueRef Context) {
192   DIBuilder *D = unwrap(Dref);
193   DIDerivedType DT =
194       D->createTypedef(unwrapDI<DIType>(Ty), Name, unwrapDI<DIFile>(File), Line,
195                        unwrapDI<DIDescriptor>(Context));
196   return wrapDI(DT);
197 }
198
199 LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, int64_t Lo,
200                                               int64_t Count) {
201   DIBuilder *D = unwrap(Dref);
202   DISubrange S = D->getOrCreateSubrange(Lo, Count);
203   return wrapDI(S);
204 }
205
206 LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
207                                            LLVMValueRef *Data, size_t Length) {
208   DIBuilder *D = unwrap(Dref);
209   DIArray A = D->getOrCreateArray(unwrapMetadataArray(Data, Length));
210   return wrapDI(A);
211 }
212
213 LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
214                                                LLVMValueRef *Data,
215                                                size_t Length) {
216   DIBuilder *D = unwrap(Dref);
217   DITypeArray A = D->getOrCreateTypeArray(unwrapMetadataArray(Data, Length));
218   return wrapDI(A);
219 }
220
221 LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr,
222                                            size_t Length) {
223   DIBuilder *D = unwrap(Dref);
224   DIExpression Expr = D->createExpression(ArrayRef<int64_t>(Addr, Length));
225   return wrapDI(Expr);
226 }
227
228 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
229                                              LLVMValueRef Storage,
230                                              LLVMValueRef VarInfo,
231                                              LLVMValueRef Expr,
232                                              LLVMBasicBlockRef Block) {
233   DIBuilder *D = unwrap(Dref);
234   Instruction *Instr =
235       D->insertDeclare(unwrap(Storage), unwrapDI<DIVariable>(VarInfo),
236                        unwrapDI<DIExpression>(Expr), unwrap(Block));
237   return wrap(Instr);
238 }
239
240 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
241                                            LLVMValueRef Val, uint64_t Offset,
242                                            LLVMValueRef VarInfo,
243                                            LLVMValueRef Expr,
244                                            LLVMBasicBlockRef Block) {
245   DIBuilder *D = unwrap(Dref);
246   Instruction *Instr = D->insertDbgValueIntrinsic(
247       unwrap(Val), Offset, unwrapDI<DIVariable>(VarInfo),
248       unwrapDI<DIExpression>(Expr), unwrap(Block));
249   return wrap(Instr);
250 }