d9d0f7ac96488fce9b637c41a944f2f79c43727c
[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 #include "IRBindings.h"
16 #include "llvm/IR/DIBuilder.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/Module.h"
19
20 using namespace llvm;
21
22 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
23
24 LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) {
25   Module *m = unwrap(mref);
26   return wrap(new DIBuilder(*m));
27 }
28
29 void LLVMDIBuilderDestroy(LLVMDIBuilderRef dref) {
30   DIBuilder *d = unwrap(dref);
31   delete d;
32 }
33
34 void LLVMDIBuilderFinalize(LLVMDIBuilderRef dref) { unwrap(dref)->finalize(); }
35
36 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
37                                                unsigned Lang, const char *File,
38                                                const char *Dir,
39                                                const char *Producer,
40                                                int Optimized, const char *Flags,
41                                                unsigned RuntimeVersion) {
42   DIBuilder *D = unwrap(Dref);
43   return wrap(D->createCompileUnit(Lang, File, Dir, Producer, Optimized, Flags,
44                                    RuntimeVersion));
45 }
46
47 LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
48                                         const char *Dir) {
49   DIBuilder *D = unwrap(Dref);
50   return wrap(D->createFile(File, Dir));
51 }
52
53 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
54                                                 LLVMMetadataRef Scope,
55                                                 LLVMMetadataRef File,
56                                                 unsigned Line,
57                                                 unsigned Column) {
58   DIBuilder *D = unwrap(Dref);
59   auto *LB = D->createLexicalBlock(unwrap<DILocalScope>(Scope),
60                                    unwrap<DIFile>(File), Line, Column);
61   return wrap(LB);
62 }
63
64 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
65                                                     LLVMMetadataRef Scope,
66                                                     LLVMMetadataRef File,
67                                                     unsigned Discriminator) {
68   DIBuilder *D = unwrap(Dref);
69   return wrap(D->createLexicalBlockFile(unwrap<DILocalScope>(Scope),
70                                         unwrap<DIFile>(File), Discriminator));
71 }
72
73 LLVMMetadataRef LLVMDIBuilderCreateFunction(
74     LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
75     const char *LinkageName, LLVMMetadataRef File, unsigned Line,
76     LLVMMetadataRef CompositeType, int IsLocalToUnit, int IsDefinition,
77     unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Func) {
78   DIBuilder *D = unwrap(Dref);
79   return wrap(D->createFunction(unwrap<DIScope>(Scope), Name, LinkageName,
80                                 File ? unwrap<DIFile>(File) : nullptr, Line,
81                                 unwrap<DISubroutineType>(CompositeType),
82                                 IsLocalToUnit, IsDefinition, ScopeLine, Flags,
83                                 IsOptimized, unwrap<Function>(Func)));
84 }
85
86 LLVMMetadataRef
87 LLVMDIBuilderCreateAutoVariable(LLVMDIBuilderRef Dref, LLVMMetadataRef Scope,
88                                 const char *Name, LLVMMetadataRef File,
89                                 unsigned Line, LLVMMetadataRef Ty,
90                                 int AlwaysPreserve, unsigned Flags) {
91   DIBuilder *D = unwrap(Dref);
92   return wrap(D->createAutoVariable(unwrap<DIScope>(Scope), Name,
93                                     unwrap<DIFile>(File), Line,
94                                     unwrap<DIType>(Ty), AlwaysPreserve, Flags));
95 }
96
97 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
98     LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
99     unsigned ArgNo, LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty,
100     int AlwaysPreserve, unsigned Flags) {
101   DIBuilder *D = unwrap(Dref);
102   return wrap(D->createParameterVariable(
103       unwrap<DIScope>(Scope), Name, ArgNo, unwrap<DIFile>(File), Line,
104       unwrap<DIType>(Ty), AlwaysPreserve, Flags));
105 }
106
107 LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
108                                              const char *Name,
109                                              uint64_t SizeInBits,
110                                              uint64_t AlignInBits,
111                                              unsigned Encoding) {
112   DIBuilder *D = unwrap(Dref);
113   return wrap(D->createBasicType(Name, SizeInBits, AlignInBits, Encoding));
114 }
115
116 LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
117                                                LLVMMetadataRef PointeeType,
118                                                uint64_t SizeInBits,
119                                                uint64_t AlignInBits,
120                                                const char *Name) {
121   DIBuilder *D = unwrap(Dref);
122   return wrap(D->createPointerType(unwrap<DIType>(PointeeType), SizeInBits,
123                                    AlignInBits, Name));
124 }
125
126 LLVMMetadataRef
127 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File,
128                                   LLVMMetadataRef ParameterTypes) {
129   DIBuilder *D = unwrap(Dref);
130   return wrap(
131       D->createSubroutineType(File ? unwrap<DIFile>(File) : nullptr,
132                               DITypeRefArray(unwrap<MDTuple>(ParameterTypes))));
133 }
134
135 LLVMMetadataRef LLVMDIBuilderCreateStructType(
136     LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
137     LLVMMetadataRef File, unsigned Line, uint64_t SizeInBits,
138     uint64_t AlignInBits, unsigned Flags, LLVMMetadataRef DerivedFrom,
139     LLVMMetadataRef ElementTypes) {
140   DIBuilder *D = unwrap(Dref);
141   return wrap(D->createStructType(
142       unwrap<DIScope>(Scope), Name, File ? unwrap<DIFile>(File) : nullptr, Line,
143       SizeInBits, AlignInBits, Flags,
144       DerivedFrom ? unwrap<DIType>(DerivedFrom) : nullptr,
145       ElementTypes ? DINodeArray(unwrap<MDTuple>(ElementTypes)) : nullptr));
146 }
147
148 LLVMMetadataRef LLVMDIBuilderCreateReplaceableCompositeType(
149     LLVMDIBuilderRef Dref, unsigned Tag, const char *Name,
150     LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
151     unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
152     unsigned Flags) {
153   DIBuilder *D = unwrap(Dref);
154   return wrap(D->createReplaceableCompositeType(
155       Tag, Name, unwrap<DIScope>(Scope), File ? unwrap<DIFile>(File) : nullptr,
156       Line, RuntimeLang, SizeInBits, AlignInBits, Flags));
157 }
158
159 LLVMMetadataRef
160 LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef Dref, LLVMMetadataRef Scope,
161                               const char *Name, LLVMMetadataRef File,
162                               unsigned Line, uint64_t SizeInBits,
163                               uint64_t AlignInBits, uint64_t OffsetInBits,
164                               unsigned Flags, LLVMMetadataRef Ty) {
165   DIBuilder *D = unwrap(Dref);
166   return wrap(D->createMemberType(
167       unwrap<DIScope>(Scope), Name, File ? unwrap<DIFile>(File) : nullptr, Line,
168       SizeInBits, AlignInBits, OffsetInBits, Flags, unwrap<DIType>(Ty)));
169 }
170
171 LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
172                                              uint64_t SizeInBits,
173                                              uint64_t AlignInBits,
174                                              LLVMMetadataRef ElementType,
175                                              LLVMMetadataRef Subscripts) {
176   DIBuilder *D = unwrap(Dref);
177   return wrap(D->createArrayType(SizeInBits, AlignInBits,
178                                  unwrap<DIType>(ElementType),
179                                  DINodeArray(unwrap<MDTuple>(Subscripts))));
180 }
181
182 LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,
183                                            LLVMMetadataRef Ty, const char *Name,
184                                            LLVMMetadataRef File, unsigned Line,
185                                            LLVMMetadataRef Context) {
186   DIBuilder *D = unwrap(Dref);
187   return wrap(D->createTypedef(unwrap<DIType>(Ty), Name,
188                                File ? unwrap<DIFile>(File) : nullptr, Line,
189                                Context ? unwrap<DIScope>(Context) : nullptr));
190 }
191
192 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
193                                                  int64_t Lo, int64_t Count) {
194   DIBuilder *D = unwrap(Dref);
195   return wrap(D->getOrCreateSubrange(Lo, Count));
196 }
197
198 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
199                                               LLVMMetadataRef *Data,
200                                               size_t Length) {
201   DIBuilder *D = unwrap(Dref);
202   Metadata **DataValue = unwrap(Data);
203   ArrayRef<Metadata *> Elements(DataValue, Length);
204   DINodeArray A = D->getOrCreateArray(Elements);
205   return wrap(A.get());
206 }
207
208 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
209                                                   LLVMMetadataRef *Data,
210                                                   size_t Length) {
211   DIBuilder *D = unwrap(Dref);
212   Metadata **DataValue = unwrap(Data);
213   ArrayRef<Metadata *> Elements(DataValue, Length);
214   DITypeRefArray A = D->getOrCreateTypeArray(Elements);
215   return wrap(A.get());
216 }
217
218 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
219                                               int64_t *Addr, size_t Length) {
220   DIBuilder *D = unwrap(Dref);
221   return wrap(D->createExpression(ArrayRef<int64_t>(Addr, Length)));
222 }
223
224 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
225                                              LLVMValueRef Storage,
226                                              LLVMMetadataRef VarInfo,
227                                              LLVMMetadataRef Expr,
228                                              LLVMBasicBlockRef Block) {
229   // Fail immediately here until the llgo folks update their bindings.  The
230   // called function is going to assert out anyway.
231   llvm_unreachable("DIBuilder API change requires a DebugLoc");
232
233   DIBuilder *D = unwrap(Dref);
234   Instruction *Instr = D->insertDeclare(
235       unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
236       unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
237   return wrap(Instr);
238 }
239
240 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
241                                            LLVMValueRef Val, uint64_t Offset,
242                                            LLVMMetadataRef VarInfo,
243                                            LLVMMetadataRef Expr,
244                                            LLVMBasicBlockRef Block) {
245   // Fail immediately here until the llgo folks update their bindings.  The
246   // called function is going to assert out anyway.
247   llvm_unreachable("DIBuilder API change requires a DebugLoc");
248
249   DIBuilder *D = unwrap(Dref);
250   Instruction *Instr = D->insertDbgValueIntrinsic(
251       unwrap(Val), Offset, unwrap<DILocalVariable>(VarInfo),
252       unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
253   return wrap(Instr);
254 }