1 //===- DIBuilderBindings.cpp - Bindings for DIBuilder ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines C bindings for the DIBuilder class.
12 //===----------------------------------------------------------------------===//
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"
22 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
24 LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) {
25 Module *m = unwrap(mref);
26 return wrap(new DIBuilder(*m));
29 void LLVMDIBuilderDestroy(LLVMDIBuilderRef dref) {
30 DIBuilder *d = unwrap(dref);
34 void LLVMDIBuilderFinalize(LLVMDIBuilderRef dref) { unwrap(dref)->finalize(); }
36 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
37 unsigned Lang, const char *File,
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);
48 LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
50 DIBuilder *D = unwrap(Dref);
51 DIFile F = D->createFile(File, Dir);
55 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
56 LLVMMetadataRef Scope,
60 DIBuilder *D = unwrap(Dref);
61 auto *LB = D->createLexicalBlock(unwrap<MDLocalScope>(Scope),
62 unwrap<MDFile>(File), Line, Column);
66 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
67 LLVMMetadataRef Scope,
69 unsigned Discriminator) {
70 DIBuilder *D = unwrap(Dref);
71 DILexicalBlockFile LBF = D->createLexicalBlockFile(
72 unwrap<MDLocalScope>(Scope), unwrap<MDFile>(File), Discriminator);
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));
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);
101 LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
104 uint64_t AlignInBits,
106 DIBuilder *D = unwrap(Dref);
107 return wrap(D->createBasicType(Name, SizeInBits, AlignInBits, Encoding));
110 LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
111 LLVMMetadataRef PointeeType,
113 uint64_t AlignInBits,
115 DIBuilder *D = unwrap(Dref);
116 return wrap(D->createPointerType(unwrap<MDType>(PointeeType), SizeInBits,
121 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File,
122 LLVMMetadataRef ParameterTypes) {
123 DIBuilder *D = unwrap(Dref);
125 D->createSubroutineType(File ? unwrap<MDFile>(File) : nullptr,
126 DITypeArray(unwrap<MDTuple>(ParameterTypes))));
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<MDScope>(Scope), Name, File ? unwrap<MDFile>(File) : nullptr, Line,
137 SizeInBits, AlignInBits, Flags,
138 DerivedFrom ? unwrap<MDType>(DerivedFrom) : nullptr,
139 ElementTypes ? DIArray(unwrap<MDTuple>(ElementTypes)) : nullptr));
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,
147 DIBuilder *D = unwrap(Dref);
148 return wrap(D->createReplaceableCompositeType(
149 Tag, Name, unwrap<MDScope>(Scope), File ? unwrap<MDFile>(File) : nullptr,
150 Line, RuntimeLang, SizeInBits, AlignInBits, Flags));
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<MDScope>(Scope), Name, File ? unwrap<MDFile>(File) : nullptr, Line,
162 SizeInBits, AlignInBits, OffsetInBits, Flags, unwrap<MDType>(Ty)));
165 LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
167 uint64_t AlignInBits,
168 LLVMMetadataRef ElementType,
169 LLVMMetadataRef Subscripts) {
170 DIBuilder *D = unwrap(Dref);
171 return wrap(D->createArrayType(SizeInBits, AlignInBits,
172 unwrap<MDType>(ElementType),
173 DIArray(unwrap<MDTuple>(Subscripts))));
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<MDType>(Ty), Name,
182 File ? unwrap<MDFile>(File) : nullptr, Line,
183 Context ? unwrap<MDScope>(Context) : nullptr));
186 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
187 int64_t Lo, int64_t Count) {
188 DIBuilder *D = unwrap(Dref);
189 DISubrange S = D->getOrCreateSubrange(Lo, Count);
193 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
194 LLVMMetadataRef *Data,
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.get());
203 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
204 LLVMMetadataRef *Data,
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.get());
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));
220 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
221 LLVMValueRef Storage,
222 LLVMMetadataRef VarInfo,
223 LLVMMetadataRef Expr,
224 LLVMBasicBlockRef Block) {
225 // Fail immediately here until the llgo folks update their bindings. The
226 // called function is going to assert out anyway.
227 llvm_unreachable("DIBuilder API change requires a DebugLoc");
229 DIBuilder *D = unwrap(Dref);
230 Instruction *Instr = D->insertDeclare(
231 unwrap(Storage), unwrap<MDLocalVariable>(VarInfo),
232 unwrap<MDExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
236 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
237 LLVMValueRef Val, uint64_t Offset,
238 LLVMMetadataRef VarInfo,
239 LLVMMetadataRef Expr,
240 LLVMBasicBlockRef Block) {
241 // Fail immediately here until the llgo folks update their bindings. The
242 // called function is going to assert out anyway.
243 llvm_unreachable("DIBuilder API change requires a DebugLoc");
245 DIBuilder *D = unwrap(Dref);
246 Instruction *Instr = D->insertDbgValueIntrinsic(
247 unwrap(Val), Offset, unwrap<MDLocalVariable>(VarInfo),
248 unwrap<MDExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));