df5885de25c49e2d958a71c664057befeba9af3a
[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 LLVMDIBuilderCreateLocalVariable(
87     LLVMDIBuilderRef Dref, unsigned Tag, LLVMMetadataRef Scope,
88     const char *Name, LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty,
89     int AlwaysPreserve, unsigned Flags, unsigned ArgNo) {
90   DIBuilder *D = unwrap(Dref);
91   return wrap(D->createLocalVariable(
92       Tag, unwrap<DIScope>(Scope), Name, unwrap<DIFile>(File), Line,
93       unwrap<DIType>(Ty), AlwaysPreserve, Flags, ArgNo));
94 }
95
96 LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
97                                              const char *Name,
98                                              uint64_t SizeInBits,
99                                              uint64_t AlignInBits,
100                                              unsigned Encoding) {
101   DIBuilder *D = unwrap(Dref);
102   return wrap(D->createBasicType(Name, SizeInBits, AlignInBits, Encoding));
103 }
104
105 LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
106                                                LLVMMetadataRef PointeeType,
107                                                uint64_t SizeInBits,
108                                                uint64_t AlignInBits,
109                                                const char *Name) {
110   DIBuilder *D = unwrap(Dref);
111   return wrap(D->createPointerType(unwrap<DIType>(PointeeType), SizeInBits,
112                                    AlignInBits, Name));
113 }
114
115 LLVMMetadataRef
116 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File,
117                                   LLVMMetadataRef ParameterTypes) {
118   DIBuilder *D = unwrap(Dref);
119   return wrap(
120       D->createSubroutineType(File ? unwrap<DIFile>(File) : nullptr,
121                               DITypeRefArray(unwrap<MDTuple>(ParameterTypes))));
122 }
123
124 LLVMMetadataRef LLVMDIBuilderCreateStructType(
125     LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
126     LLVMMetadataRef File, unsigned Line, uint64_t SizeInBits,
127     uint64_t AlignInBits, unsigned Flags, LLVMMetadataRef DerivedFrom,
128     LLVMMetadataRef ElementTypes) {
129   DIBuilder *D = unwrap(Dref);
130   return wrap(D->createStructType(
131       unwrap<DIScope>(Scope), Name, File ? unwrap<DIFile>(File) : nullptr, Line,
132       SizeInBits, AlignInBits, Flags,
133       DerivedFrom ? unwrap<DIType>(DerivedFrom) : nullptr,
134       ElementTypes ? DINodeArray(unwrap<MDTuple>(ElementTypes)) : nullptr));
135 }
136
137 LLVMMetadataRef LLVMDIBuilderCreateReplaceableCompositeType(
138     LLVMDIBuilderRef Dref, unsigned Tag, const char *Name,
139     LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
140     unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
141     unsigned Flags) {
142   DIBuilder *D = unwrap(Dref);
143   return wrap(D->createReplaceableCompositeType(
144       Tag, Name, unwrap<DIScope>(Scope), File ? unwrap<DIFile>(File) : nullptr,
145       Line, RuntimeLang, SizeInBits, AlignInBits, Flags));
146 }
147
148 LLVMMetadataRef
149 LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef Dref, LLVMMetadataRef Scope,
150                               const char *Name, LLVMMetadataRef File,
151                               unsigned Line, uint64_t SizeInBits,
152                               uint64_t AlignInBits, uint64_t OffsetInBits,
153                               unsigned Flags, LLVMMetadataRef Ty) {
154   DIBuilder *D = unwrap(Dref);
155   return wrap(D->createMemberType(
156       unwrap<DIScope>(Scope), Name, File ? unwrap<DIFile>(File) : nullptr, Line,
157       SizeInBits, AlignInBits, OffsetInBits, Flags, unwrap<DIType>(Ty)));
158 }
159
160 LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
161                                              uint64_t SizeInBits,
162                                              uint64_t AlignInBits,
163                                              LLVMMetadataRef ElementType,
164                                              LLVMMetadataRef Subscripts) {
165   DIBuilder *D = unwrap(Dref);
166   return wrap(D->createArrayType(SizeInBits, AlignInBits,
167                                  unwrap<DIType>(ElementType),
168                                  DINodeArray(unwrap<MDTuple>(Subscripts))));
169 }
170
171 LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,
172                                            LLVMMetadataRef Ty, const char *Name,
173                                            LLVMMetadataRef File, unsigned Line,
174                                            LLVMMetadataRef Context) {
175   DIBuilder *D = unwrap(Dref);
176   return wrap(D->createTypedef(unwrap<DIType>(Ty), Name,
177                                File ? unwrap<DIFile>(File) : nullptr, Line,
178                                Context ? unwrap<DIScope>(Context) : nullptr));
179 }
180
181 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
182                                                  int64_t Lo, int64_t Count) {
183   DIBuilder *D = unwrap(Dref);
184   return wrap(D->getOrCreateSubrange(Lo, Count));
185 }
186
187 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
188                                               LLVMMetadataRef *Data,
189                                               size_t Length) {
190   DIBuilder *D = unwrap(Dref);
191   Metadata **DataValue = unwrap(Data);
192   ArrayRef<Metadata *> Elements(DataValue, Length);
193   DINodeArray A = D->getOrCreateArray(Elements);
194   return wrap(A.get());
195 }
196
197 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
198                                                   LLVMMetadataRef *Data,
199                                                   size_t Length) {
200   DIBuilder *D = unwrap(Dref);
201   Metadata **DataValue = unwrap(Data);
202   ArrayRef<Metadata *> Elements(DataValue, Length);
203   DITypeRefArray A = D->getOrCreateTypeArray(Elements);
204   return wrap(A.get());
205 }
206
207 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
208                                               int64_t *Addr, size_t Length) {
209   DIBuilder *D = unwrap(Dref);
210   return wrap(D->createExpression(ArrayRef<int64_t>(Addr, Length)));
211 }
212
213 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
214                                              LLVMValueRef Storage,
215                                              LLVMMetadataRef VarInfo,
216                                              LLVMMetadataRef Expr,
217                                              LLVMBasicBlockRef Block) {
218   // Fail immediately here until the llgo folks update their bindings.  The
219   // called function is going to assert out anyway.
220   llvm_unreachable("DIBuilder API change requires a DebugLoc");
221
222   DIBuilder *D = unwrap(Dref);
223   Instruction *Instr = D->insertDeclare(
224       unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
225       unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
226   return wrap(Instr);
227 }
228
229 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
230                                            LLVMValueRef Val, uint64_t Offset,
231                                            LLVMMetadataRef VarInfo,
232                                            LLVMMetadataRef Expr,
233                                            LLVMBasicBlockRef Block) {
234   // Fail immediately here until the llgo folks update their bindings.  The
235   // called function is going to assert out anyway.
236   llvm_unreachable("DIBuilder API change requires a DebugLoc");
237
238   DIBuilder *D = unwrap(Dref);
239   Instruction *Instr = D->insertDbgValueIntrinsic(
240       unwrap(Val), Offset, unwrap<DILocalVariable>(VarInfo),
241       unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
242   return wrap(Instr);
243 }