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