[InstCombine] Don't assume m_Mul gives back an Instruction
[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, 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   // FIXME: Update the Go bindings to match the DIBuilder API.
92   if (ArgNo)
93     return wrap(D->createParameterVariable(
94         unwrap<DIScope>(Scope), Name, ArgNo, unwrap<DIFile>(File), Line,
95         unwrap<DIType>(Ty), AlwaysPreserve, Flags));
96   return wrap(D->createAutoVariable(unwrap<DIScope>(Scope), Name,
97                                     unwrap<DIFile>(File), Line,
98                                     unwrap<DIType>(Ty), AlwaysPreserve, Flags));
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<DIType>(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<DIFile>(File) : nullptr,
126                               DITypeRefArray(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<DIScope>(Scope), Name, File ? unwrap<DIFile>(File) : nullptr, Line,
137       SizeInBits, AlignInBits, Flags,
138       DerivedFrom ? unwrap<DIType>(DerivedFrom) : nullptr,
139       ElementTypes ? DINodeArray(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<DIScope>(Scope), File ? unwrap<DIFile>(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<DIScope>(Scope), Name, File ? unwrap<DIFile>(File) : nullptr, Line,
162       SizeInBits, AlignInBits, OffsetInBits, Flags, unwrap<DIType>(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<DIType>(ElementType),
173                                  DINodeArray(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<DIType>(Ty), Name,
182                                File ? unwrap<DIFile>(File) : nullptr, Line,
183                                Context ? unwrap<DIScope>(Context) : nullptr));
184 }
185
186 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
187                                                  int64_t Lo, int64_t Count) {
188   DIBuilder *D = unwrap(Dref);
189   return wrap(D->getOrCreateSubrange(Lo, Count));
190 }
191
192 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
193                                               LLVMMetadataRef *Data,
194                                               size_t Length) {
195   DIBuilder *D = unwrap(Dref);
196   Metadata **DataValue = unwrap(Data);
197   ArrayRef<Metadata *> Elements(DataValue, Length);
198   DINodeArray A = D->getOrCreateArray(Elements);
199   return wrap(A.get());
200 }
201
202 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
203                                                   LLVMMetadataRef *Data,
204                                                   size_t Length) {
205   DIBuilder *D = unwrap(Dref);
206   Metadata **DataValue = unwrap(Data);
207   ArrayRef<Metadata *> Elements(DataValue, Length);
208   DITypeRefArray A = D->getOrCreateTypeArray(Elements);
209   return wrap(A.get());
210 }
211
212 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
213                                               int64_t *Addr, size_t Length) {
214   DIBuilder *D = unwrap(Dref);
215   return wrap(D->createExpression(ArrayRef<int64_t>(Addr, Length)));
216 }
217
218 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
219                                              LLVMValueRef Storage,
220                                              LLVMMetadataRef VarInfo,
221                                              LLVMMetadataRef Expr,
222                                              LLVMBasicBlockRef Block) {
223   // Fail immediately here until the llgo folks update their bindings.  The
224   // called function is going to assert out anyway.
225   llvm_unreachable("DIBuilder API change requires a DebugLoc");
226
227   DIBuilder *D = unwrap(Dref);
228   Instruction *Instr = D->insertDeclare(
229       unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
230       unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
231   return wrap(Instr);
232 }
233
234 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
235                                            LLVMValueRef Val, uint64_t Offset,
236                                            LLVMMetadataRef VarInfo,
237                                            LLVMMetadataRef Expr,
238                                            LLVMBasicBlockRef Block) {
239   // Fail immediately here until the llgo folks update their bindings.  The
240   // called function is going to assert out anyway.
241   llvm_unreachable("DIBuilder API change requires a DebugLoc");
242
243   DIBuilder *D = unwrap(Dref);
244   Instruction *Instr = D->insertDbgValueIntrinsic(
245       unwrap(Val), Offset, unwrap<DILocalVariable>(VarInfo),
246       unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
247   return wrap(Instr);
248 }