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