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