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