Reformat with gofmt.
[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
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/DIBuilder.h"
18
19 using namespace llvm;
20
21 namespace {
22 template <typename T> T unwrapDI(LLVMValueRef v) {
23   return v ? T(unwrap<MDNode>(v)) : T();
24 }
25 }
26
27 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
28
29 LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) {
30   Module *m = unwrap(mref);
31   return wrap(new DIBuilder(*m));
32 }
33
34 void LLVMDIBuilderDestroy(LLVMDIBuilderRef dref) {
35   DIBuilder *d = unwrap(dref);
36   delete d;
37 }
38
39 void LLVMDIBuilderFinalize(LLVMDIBuilderRef dref) { unwrap(dref)->finalize(); }
40
41 LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
42                                             unsigned Lang, const char *File,
43                                             const char *Dir,
44                                             const char *Producer, int Optimized,
45                                             const char *Flags,
46                                             unsigned RuntimeVersion) {
47   DIBuilder *D = unwrap(Dref);
48   DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized,
49                                           Flags, RuntimeVersion);
50   return wrap(CU);
51 }
52
53 LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
54                                      const char *Dir) {
55   DIBuilder *D = unwrap(Dref);
56   DIFile F = D->createFile(File, Dir);
57   return wrap(F);
58 }
59
60 LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
61                                              LLVMValueRef Scope,
62                                              LLVMValueRef File, unsigned Line,
63                                              unsigned Column) {
64   DIBuilder *D = unwrap(Dref);
65   DILexicalBlock LB = D->createLexicalBlock(
66       unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Line, Column);
67   return wrap(LB);
68 }
69
70 LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
71                                                  LLVMValueRef Scope,
72                                                  LLVMValueRef File,
73                                                  unsigned Discriminator) {
74   DIBuilder *D = unwrap(Dref);
75   DILexicalBlockFile LBF = D->createLexicalBlockFile(
76       unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Discriminator);
77   return wrap(LBF);
78 }
79
80 LLVMValueRef LLVMDIBuilderCreateFunction(
81     LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
82     const char *LinkageName, LLVMValueRef File, unsigned Line,
83     LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition,
84     unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Func) {
85   DIBuilder *D = unwrap(Dref);
86   DISubprogram SP = D->createFunction(
87       unwrapDI<DIDescriptor>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
88       Line, unwrapDI<DICompositeType>(CompositeType), IsLocalToUnit,
89       IsDefinition, ScopeLine, Flags, IsOptimized, unwrap<Function>(Func));
90   return wrap(SP);
91 }
92
93 LLVMValueRef LLVMDIBuilderCreateLocalVariable(
94     LLVMDIBuilderRef Dref, unsigned Tag, LLVMValueRef Scope, const char *Name,
95     LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve,
96     unsigned Flags, unsigned ArgNo) {
97   DIBuilder *D = unwrap(Dref);
98   DIVariable V = D->createLocalVariable(
99       Tag, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
100       unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, ArgNo);
101   return wrap(V);
102 }
103
104 LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
105                                           const char *Name, 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 LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
114                                             LLVMValueRef PointeeType,
115                                             uint64_t SizeInBits,
116                                             uint64_t AlignInBits,
117                                             const char *Name) {
118   DIBuilder *D = unwrap(Dref);
119   DIDerivedType T = D->createPointerType(unwrapDI<DIType>(PointeeType),
120                                          SizeInBits, AlignInBits, Name);
121   return wrap(T);
122 }
123
124 LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref,
125                                                LLVMValueRef File,
126                                                LLVMValueRef ParameterTypes) {
127   DIBuilder *D = unwrap(Dref);
128   DICompositeType CT = D->createSubroutineType(
129       unwrapDI<DIFile>(File), unwrapDI<DITypeArray>(ParameterTypes));
130   return wrap(CT);
131 }
132
133 LLVMValueRef LLVMDIBuilderCreateStructType(
134     LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
135     LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
136     unsigned Flags, LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes) {
137   DIBuilder *D = unwrap(Dref);
138   DICompositeType CT = D->createStructType(
139       unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
140       SizeInBits, AlignInBits, Flags, unwrapDI<DIType>(DerivedFrom),
141       unwrapDI<DIArray>(ElementTypes));
142   return wrap(CT);
143 }
144
145 LLVMValueRef LLVMDIBuilderCreateMemberType(
146     LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
147     LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
148     uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty) {
149   DIBuilder *D = unwrap(Dref);
150   DIDerivedType DT = D->createMemberType(
151       unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
152       SizeInBits, AlignInBits, OffsetInBits, Flags, unwrapDI<DIType>(Ty));
153   return wrap(DT);
154 }
155
156 LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
157                                           uint64_t SizeInBits,
158                                           uint64_t AlignInBits,
159                                           LLVMValueRef ElementType,
160                                           LLVMValueRef Subscripts) {
161   DIBuilder *D = unwrap(Dref);
162   DICompositeType CT =
163       D->createArrayType(SizeInBits, AlignInBits, unwrapDI<DIType>(ElementType),
164                          unwrapDI<DIArray>(Subscripts));
165   return wrap(CT);
166 }
167
168 LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty,
169                                         const char *Name, LLVMValueRef File,
170                                         unsigned Line, LLVMValueRef Context) {
171   DIBuilder *D = unwrap(Dref);
172   DIDerivedType DT =
173       D->createTypedef(unwrapDI<DIType>(Ty), Name, unwrapDI<DIFile>(File), Line,
174                        unwrapDI<DIDescriptor>(Context));
175   return wrap(DT);
176 }
177
178 LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, int64_t Lo,
179                                               int64_t Count) {
180   DIBuilder *D = unwrap(Dref);
181   DISubrange S = D->getOrCreateSubrange(Lo, Count);
182   return wrap(S);
183 }
184
185 LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
186                                            LLVMValueRef *Data, size_t Length) {
187   DIBuilder *D = unwrap(Dref);
188   Value **DataValue = unwrap(Data);
189   ArrayRef<Value *> Elements(DataValue, Length);
190   DIArray A = D->getOrCreateArray(Elements);
191   return wrap(A);
192 }
193
194 LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
195                                                LLVMValueRef *Data,
196                                                size_t Length) {
197   DIBuilder *D = unwrap(Dref);
198   Value **DataValue = unwrap(Data);
199   ArrayRef<Value *> Elements(DataValue, Length);
200   DITypeArray A = D->getOrCreateTypeArray(Elements);
201   return wrap(A);
202 }
203
204 LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr,
205                                            size_t Length) {
206   DIBuilder *D = unwrap(Dref);
207   DIExpression Expr = D->createExpression(ArrayRef<int64_t>(Addr, Length));
208   return wrap(Expr);
209 }
210
211 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
212                                              LLVMValueRef Storage,
213                                              LLVMValueRef VarInfo,
214                                              LLVMValueRef Expr,
215                                              LLVMBasicBlockRef Block) {
216   DIBuilder *D = unwrap(Dref);
217   Instruction *Instr =
218       D->insertDeclare(unwrap(Storage), unwrapDI<DIVariable>(VarInfo),
219                        unwrapDI<DIExpression>(Expr), unwrap(Block));
220   return wrap(Instr);
221 }
222
223 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
224                                            LLVMValueRef Val, uint64_t Offset,
225                                            LLVMValueRef VarInfo,
226                                            LLVMValueRef Expr,
227                                            LLVMBasicBlockRef Block) {
228   DIBuilder *D = unwrap(Dref);
229   Instruction *Instr = D->insertDbgValueIntrinsic(
230       unwrap(Val), Offset, unwrapDI<DIVariable>(VarInfo),
231       unwrapDI<DIExpression>(Expr), unwrap(Block));
232   return wrap(Instr);
233 }