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 
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder,LLVMDIBuilderRef)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 
LLVMDIBuilderDestroy(LLVMDIBuilderRef dref)29 void LLVMDIBuilderDestroy(LLVMDIBuilderRef dref) {
30   DIBuilder *d = unwrap(dref);
31   delete d;
32 }
33 
LLVMDIBuilderFinalize(LLVMDIBuilderRef dref)34 void LLVMDIBuilderFinalize(LLVMDIBuilderRef dref) { unwrap(dref)->finalize(); }
35 
LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,unsigned Lang,const char * File,const char * Dir,const char * Producer,int Optimized,const char * Flags,unsigned RuntimeVersion)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 
LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref,const char * File,const char * Dir)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 
LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,LLVMMetadataRef Scope,LLVMMetadataRef File,unsigned Line,unsigned Column)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 
LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,LLVMMetadataRef Scope,LLVMMetadataRef File,unsigned Discriminator)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 
LLVMDIBuilderCreateFunction(LLVMDIBuilderRef Dref,LLVMMetadataRef Scope,const char * Name,const char * LinkageName,LLVMMetadataRef File,unsigned Line,LLVMMetadataRef CompositeType,int IsLocalToUnit,int IsDefinition,unsigned ScopeLine,unsigned Flags,int IsOptimized,LLVMValueRef Func)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 
LLVMDIBuilderCreateLocalVariable(LLVMDIBuilderRef Dref,unsigned Tag,LLVMMetadataRef Scope,const char * Name,LLVMMetadataRef File,unsigned Line,LLVMMetadataRef Ty,int AlwaysPreserve,unsigned Flags,unsigned ArgNo)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 
LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,const char * Name,uint64_t SizeInBits,uint64_t AlignInBits,unsigned Encoding)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   DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding);
108   return wrap(T);
109 }
110 
LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,LLVMMetadataRef PointeeType,uint64_t SizeInBits,uint64_t AlignInBits,const char * Name)111 LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
112                                                LLVMMetadataRef PointeeType,
113                                                uint64_t SizeInBits,
114                                                uint64_t AlignInBits,
115                                                const char *Name) {
116   DIBuilder *D = unwrap(Dref);
117   DIDerivedType T = D->createPointerType(unwrap<MDType>(PointeeType),
118                                          SizeInBits, AlignInBits, Name);
119   return wrap(T);
120 }
121 
122 LLVMMetadataRef
LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref,LLVMMetadataRef File,LLVMMetadataRef ParameterTypes)123 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File,
124                                   LLVMMetadataRef ParameterTypes) {
125   DIBuilder *D = unwrap(Dref);
126   DICompositeType CT =
127       D->createSubroutineType(File ? unwrap<MDFile>(File) : nullptr,
128                               DITypeArray(unwrap<MDTuple>(ParameterTypes)));
129   return wrap(CT);
130 }
131 
LLVMDIBuilderCreateStructType(LLVMDIBuilderRef Dref,LLVMMetadataRef Scope,const char * Name,LLVMMetadataRef File,unsigned Line,uint64_t SizeInBits,uint64_t AlignInBits,unsigned Flags,LLVMMetadataRef DerivedFrom,LLVMMetadataRef ElementTypes)132 LLVMMetadataRef LLVMDIBuilderCreateStructType(
133     LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
134     LLVMMetadataRef File, unsigned Line, uint64_t SizeInBits,
135     uint64_t AlignInBits, unsigned Flags, LLVMMetadataRef DerivedFrom,
136     LLVMMetadataRef ElementTypes) {
137   DIBuilder *D = unwrap(Dref);
138   DICompositeType CT = D->createStructType(
139       unwrap<MDScope>(Scope), Name, File ? unwrap<MDFile>(File) : nullptr, Line,
140       SizeInBits, AlignInBits, Flags,
141       DerivedFrom ? unwrap<MDType>(DerivedFrom) : nullptr,
142       ElementTypes ? DIArray(unwrap<MDTuple>(ElementTypes)) : nullptr);
143   return wrap(CT);
144 }
145 
LLVMDIBuilderCreateReplaceableCompositeType(LLVMDIBuilderRef Dref,unsigned Tag,const char * Name,LLVMMetadataRef Scope,LLVMMetadataRef File,unsigned Line,unsigned RuntimeLang,uint64_t SizeInBits,uint64_t AlignInBits,unsigned Flags)146 LLVMMetadataRef LLVMDIBuilderCreateReplaceableCompositeType(
147     LLVMDIBuilderRef Dref, unsigned Tag, const char *Name,
148     LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
149     unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
150     unsigned Flags) {
151   DIBuilder *D = unwrap(Dref);
152   DICompositeType CT = D->createReplaceableCompositeType(
153       Tag, Name, unwrap<MDScope>(Scope), File ? unwrap<MDFile>(File) : nullptr,
154       Line, RuntimeLang, SizeInBits, AlignInBits, Flags);
155   return wrap(CT);
156 }
157 
158 LLVMMetadataRef
LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef Dref,LLVMMetadataRef Scope,const char * Name,LLVMMetadataRef File,unsigned Line,uint64_t SizeInBits,uint64_t AlignInBits,uint64_t OffsetInBits,unsigned Flags,LLVMMetadataRef Ty)159 LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef Dref, LLVMMetadataRef Scope,
160                               const char *Name, LLVMMetadataRef File,
161                               unsigned Line, uint64_t SizeInBits,
162                               uint64_t AlignInBits, uint64_t OffsetInBits,
163                               unsigned Flags, LLVMMetadataRef Ty) {
164   DIBuilder *D = unwrap(Dref);
165   DIDerivedType DT = D->createMemberType(
166       unwrap<MDScope>(Scope), Name, File ? unwrap<MDFile>(File) : nullptr, Line,
167       SizeInBits, AlignInBits, OffsetInBits, Flags, unwrap<MDType>(Ty));
168   return wrap(DT);
169 }
170 
LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,uint64_t SizeInBits,uint64_t AlignInBits,LLVMMetadataRef ElementType,LLVMMetadataRef Subscripts)171 LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
172                                              uint64_t SizeInBits,
173                                              uint64_t AlignInBits,
174                                              LLVMMetadataRef ElementType,
175                                              LLVMMetadataRef Subscripts) {
176   DIBuilder *D = unwrap(Dref);
177   DICompositeType CT =
178       D->createArrayType(SizeInBits, AlignInBits, unwrap<MDType>(ElementType),
179                          DIArray(unwrap<MDTuple>(Subscripts)));
180   return wrap(CT);
181 }
182 
LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,LLVMMetadataRef Ty,const char * Name,LLVMMetadataRef File,unsigned Line,LLVMMetadataRef Context)183 LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,
184                                            LLVMMetadataRef Ty, const char *Name,
185                                            LLVMMetadataRef File, unsigned Line,
186                                            LLVMMetadataRef Context) {
187   DIBuilder *D = unwrap(Dref);
188   DIDerivedType DT = D->createTypedef(
189       unwrap<MDType>(Ty), Name, File ? unwrap<MDFile>(File) : nullptr, Line,
190       Context ? unwrap<MDScope>(Context) : nullptr);
191   return wrap(DT);
192 }
193 
LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,int64_t Lo,int64_t Count)194 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
195                                                  int64_t Lo, int64_t Count) {
196   DIBuilder *D = unwrap(Dref);
197   DISubrange S = D->getOrCreateSubrange(Lo, Count);
198   return wrap(S);
199 }
200 
LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,LLVMMetadataRef * Data,size_t Length)201 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
202                                               LLVMMetadataRef *Data,
203                                               size_t Length) {
204   DIBuilder *D = unwrap(Dref);
205   Metadata **DataValue = unwrap(Data);
206   ArrayRef<Metadata *> Elements(DataValue, Length);
207   DIArray A = D->getOrCreateArray(Elements);
208   return wrap(A.get());
209 }
210 
LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,LLVMMetadataRef * Data,size_t Length)211 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
212                                                   LLVMMetadataRef *Data,
213                                                   size_t Length) {
214   DIBuilder *D = unwrap(Dref);
215   Metadata **DataValue = unwrap(Data);
216   ArrayRef<Metadata *> Elements(DataValue, Length);
217   DITypeArray A = D->getOrCreateTypeArray(Elements);
218   return wrap(A.get());
219 }
220 
LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,int64_t * Addr,size_t Length)221 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
222                                               int64_t *Addr, size_t Length) {
223   DIBuilder *D = unwrap(Dref);
224   DIExpression Expr = D->createExpression(ArrayRef<int64_t>(Addr, Length));
225   return wrap(Expr);
226 }
227 
LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,LLVMValueRef Storage,LLVMMetadataRef VarInfo,LLVMMetadataRef Expr,LLVMBasicBlockRef Block)228 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
229                                              LLVMValueRef Storage,
230                                              LLVMMetadataRef VarInfo,
231                                              LLVMMetadataRef Expr,
232                                              LLVMBasicBlockRef Block) {
233   // Fail immediately here until the llgo folks update their bindings.  The
234   // called function is going to assert out anyway.
235   llvm_unreachable("DIBuilder API change requires a DebugLoc");
236 
237   DIBuilder *D = unwrap(Dref);
238   Instruction *Instr = D->insertDeclare(
239       unwrap(Storage), unwrap<MDLocalVariable>(VarInfo),
240       unwrap<MDExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
241   return wrap(Instr);
242 }
243 
LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,LLVMValueRef Val,uint64_t Offset,LLVMMetadataRef VarInfo,LLVMMetadataRef Expr,LLVMBasicBlockRef Block)244 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
245                                            LLVMValueRef Val, uint64_t Offset,
246                                            LLVMMetadataRef VarInfo,
247                                            LLVMMetadataRef Expr,
248                                            LLVMBasicBlockRef Block) {
249   // Fail immediately here until the llgo folks update their bindings.  The
250   // called function is going to assert out anyway.
251   llvm_unreachable("DIBuilder API change requires a DebugLoc");
252 
253   DIBuilder *D = unwrap(Dref);
254   Instruction *Instr = D->insertDbgValueIntrinsic(
255       unwrap(Val), Offset, unwrap<MDLocalVariable>(VarInfo),
256       unwrap<MDExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
257   return wrap(Instr);
258 }
259