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 DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding);
111 LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
112 LLVMMetadataRef PointeeType,
114 uint64_t AlignInBits,
116 DIBuilder *D = unwrap(Dref);
117 DIDerivedType T = D->createPointerType(unwrap<MDType>(PointeeType),
118 SizeInBits, AlignInBits, Name);
123 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File,
124 LLVMMetadataRef ParameterTypes) {
125 DIBuilder *D = unwrap(Dref);
127 D->createSubroutineType(File ? unwrap<MDFile>(File) : nullptr,
128 DITypeArray(unwrap<MDTuple>(ParameterTypes)));
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);
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,
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);
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));
171 LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
173 uint64_t AlignInBits,
174 LLVMMetadataRef ElementType,
175 LLVMMetadataRef Subscripts) {
176 DIBuilder *D = unwrap(Dref);
178 D->createArrayType(SizeInBits, AlignInBits, unwrap<MDType>(ElementType),
179 DIArray(unwrap<MDTuple>(Subscripts)));
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);
194 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
195 int64_t Lo, int64_t Count) {
196 DIBuilder *D = unwrap(Dref);
197 DISubrange S = D->getOrCreateSubrange(Lo, Count);
201 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
202 LLVMMetadataRef *Data,
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());
211 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
212 LLVMMetadataRef *Data,
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());
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));
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");
237 DIBuilder *D = unwrap(Dref);
238 Instruction *Instr = D->insertDeclare(
239 unwrap(Storage), unwrap<MDLocalVariable>(VarInfo),
240 unwrap<MDExpression>(Expr), /* DebugLoc */ nullptr, unwrap(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");
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));