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   return wrap(D->createCompileUnit(Lang, File, Dir, Producer, Optimized, Flags,
44                                    RuntimeVersion));
45 }
46 
LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref,const char * File,const char * Dir)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 
LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,LLVMMetadataRef Scope,LLVMMetadataRef File,unsigned Line,unsigned Column)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 
LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,LLVMMetadataRef Scope,LLVMMetadataRef File,unsigned Discriminator)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 
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)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) {
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));
84 }
85 
86 LLVMMetadataRef
LLVMDIBuilderCreateAutoVariable(LLVMDIBuilderRef Dref,LLVMMetadataRef Scope,const char * Name,LLVMMetadataRef File,unsigned Line,LLVMMetadataRef Ty,int AlwaysPreserve,unsigned Flags)87 LLVMDIBuilderCreateAutoVariable(LLVMDIBuilderRef Dref, LLVMMetadataRef Scope,
88                                 const char *Name, LLVMMetadataRef File,
89                                 unsigned Line, LLVMMetadataRef Ty,
90                                 int AlwaysPreserve, unsigned Flags) {
91   DIBuilder *D = unwrap(Dref);
92   return wrap(D->createAutoVariable(unwrap<DIScope>(Scope), Name,
93                                     unwrap<DIFile>(File), Line,
94                                     unwrap<DIType>(Ty), AlwaysPreserve, Flags));
95 }
96 
LLVMDIBuilderCreateParameterVariable(LLVMDIBuilderRef Dref,LLVMMetadataRef Scope,const char * Name,unsigned ArgNo,LLVMMetadataRef File,unsigned Line,LLVMMetadataRef Ty,int AlwaysPreserve,unsigned Flags)97 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
98     LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
99     unsigned ArgNo, LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty,
100     int AlwaysPreserve, unsigned Flags) {
101   DIBuilder *D = unwrap(Dref);
102   return wrap(D->createParameterVariable(
103       unwrap<DIScope>(Scope), Name, ArgNo, unwrap<DIFile>(File), Line,
104       unwrap<DIType>(Ty), AlwaysPreserve, Flags));
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   return wrap(D->createBasicType(Name, SizeInBits, AlignInBits, Encoding));
114 }
115 
LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,LLVMMetadataRef PointeeType,uint64_t SizeInBits,uint64_t AlignInBits,const char * Name)116 LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
117                                                LLVMMetadataRef PointeeType,
118                                                uint64_t SizeInBits,
119                                                uint64_t AlignInBits,
120                                                const char *Name) {
121   DIBuilder *D = unwrap(Dref);
122   return wrap(D->createPointerType(unwrap<DIType>(PointeeType), SizeInBits,
123                                    AlignInBits, Name));
124 }
125 
126 LLVMMetadataRef
LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref,LLVMMetadataRef File,LLVMMetadataRef ParameterTypes)127 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File,
128                                   LLVMMetadataRef ParameterTypes) {
129   DIBuilder *D = unwrap(Dref);
130   return wrap(
131       D->createSubroutineType(DITypeRefArray(unwrap<MDTuple>(ParameterTypes))));
132 }
133 
LLVMDIBuilderCreateStructType(LLVMDIBuilderRef Dref,LLVMMetadataRef Scope,const char * Name,LLVMMetadataRef File,unsigned Line,uint64_t SizeInBits,uint64_t AlignInBits,unsigned Flags,LLVMMetadataRef DerivedFrom,LLVMMetadataRef ElementTypes)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   return wrap(D->createStructType(
141       unwrap<DIScope>(Scope), Name, File ? unwrap<DIFile>(File) : nullptr, Line,
142       SizeInBits, AlignInBits, Flags,
143       DerivedFrom ? unwrap<DIType>(DerivedFrom) : nullptr,
144       ElementTypes ? DINodeArray(unwrap<MDTuple>(ElementTypes)) : nullptr));
145 }
146 
LLVMDIBuilderCreateReplaceableCompositeType(LLVMDIBuilderRef Dref,unsigned Tag,const char * Name,LLVMMetadataRef Scope,LLVMMetadataRef File,unsigned Line,unsigned RuntimeLang,uint64_t SizeInBits,uint64_t AlignInBits,unsigned Flags)147 LLVMMetadataRef LLVMDIBuilderCreateReplaceableCompositeType(
148     LLVMDIBuilderRef Dref, unsigned Tag, const char *Name,
149     LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
150     unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
151     unsigned Flags) {
152   DIBuilder *D = unwrap(Dref);
153   return wrap(D->createReplaceableCompositeType(
154       Tag, Name, unwrap<DIScope>(Scope), File ? unwrap<DIFile>(File) : nullptr,
155       Line, RuntimeLang, SizeInBits, AlignInBits, Flags));
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   return wrap(D->createMemberType(
166       unwrap<DIScope>(Scope), Name, File ? unwrap<DIFile>(File) : nullptr, Line,
167       SizeInBits, AlignInBits, OffsetInBits, Flags, unwrap<DIType>(Ty)));
168 }
169 
LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,uint64_t SizeInBits,uint64_t AlignInBits,LLVMMetadataRef ElementType,LLVMMetadataRef Subscripts)170 LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
171                                              uint64_t SizeInBits,
172                                              uint64_t AlignInBits,
173                                              LLVMMetadataRef ElementType,
174                                              LLVMMetadataRef Subscripts) {
175   DIBuilder *D = unwrap(Dref);
176   return wrap(D->createArrayType(SizeInBits, AlignInBits,
177                                  unwrap<DIType>(ElementType),
178                                  DINodeArray(unwrap<MDTuple>(Subscripts))));
179 }
180 
LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,LLVMMetadataRef Ty,const char * Name,LLVMMetadataRef File,unsigned Line,LLVMMetadataRef Context)181 LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,
182                                            LLVMMetadataRef Ty, const char *Name,
183                                            LLVMMetadataRef File, unsigned Line,
184                                            LLVMMetadataRef Context) {
185   DIBuilder *D = unwrap(Dref);
186   return wrap(D->createTypedef(unwrap<DIType>(Ty), Name,
187                                File ? unwrap<DIFile>(File) : nullptr, Line,
188                                Context ? unwrap<DIScope>(Context) : nullptr));
189 }
190 
LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,int64_t Lo,int64_t Count)191 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
192                                                  int64_t Lo, int64_t Count) {
193   DIBuilder *D = unwrap(Dref);
194   return wrap(D->getOrCreateSubrange(Lo, Count));
195 }
196 
LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,LLVMMetadataRef * Data,size_t Length)197 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
198                                               LLVMMetadataRef *Data,
199                                               size_t Length) {
200   DIBuilder *D = unwrap(Dref);
201   Metadata **DataValue = unwrap(Data);
202   ArrayRef<Metadata *> Elements(DataValue, Length);
203   DINodeArray A = D->getOrCreateArray(Elements);
204   return wrap(A.get());
205 }
206 
LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,LLVMMetadataRef * Data,size_t Length)207 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
208                                                   LLVMMetadataRef *Data,
209                                                   size_t Length) {
210   DIBuilder *D = unwrap(Dref);
211   Metadata **DataValue = unwrap(Data);
212   ArrayRef<Metadata *> Elements(DataValue, Length);
213   DITypeRefArray A = D->getOrCreateTypeArray(Elements);
214   return wrap(A.get());
215 }
216 
LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,int64_t * Addr,size_t Length)217 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
218                                               int64_t *Addr, size_t Length) {
219   DIBuilder *D = unwrap(Dref);
220   return wrap(D->createExpression(ArrayRef<int64_t>(Addr, Length)));
221 }
222 
LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,LLVMValueRef Storage,LLVMMetadataRef VarInfo,LLVMMetadataRef Expr,LLVMBasicBlockRef Block)223 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
224                                              LLVMValueRef Storage,
225                                              LLVMMetadataRef VarInfo,
226                                              LLVMMetadataRef Expr,
227                                              LLVMBasicBlockRef Block) {
228   // Fail immediately here until the llgo folks update their bindings.  The
229   // called function is going to assert out anyway.
230   llvm_unreachable("DIBuilder API change requires a DebugLoc");
231 
232   DIBuilder *D = unwrap(Dref);
233   Instruction *Instr = D->insertDeclare(
234       unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
235       unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
236   return wrap(Instr);
237 }
238 
LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,LLVMValueRef Val,uint64_t Offset,LLVMMetadataRef VarInfo,LLVMMetadataRef Expr,LLVMBasicBlockRef Block)239 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
240                                            LLVMValueRef Val, uint64_t Offset,
241                                            LLVMMetadataRef VarInfo,
242                                            LLVMMetadataRef Expr,
243                                            LLVMBasicBlockRef Block) {
244   // Fail immediately here until the llgo folks update their bindings.  The
245   // called function is going to assert out anyway.
246   llvm_unreachable("DIBuilder API change requires a DebugLoc");
247 
248   DIBuilder *D = unwrap(Dref);
249   Instruction *Instr = D->insertDbgValueIntrinsic(
250       unwrap(Val), Offset, unwrap<DILocalVariable>(VarInfo),
251       unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
252   return wrap(Instr);
253 }
254