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 return wrap(D->createCompileUnit(Lang, File, Dir, Producer, Optimized, Flags,
47 LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
49 DIBuilder *D = unwrap(Dref);
50 return wrap(D->createFile(File, Dir));
53 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
54 LLVMMetadataRef Scope,
58 DIBuilder *D = unwrap(Dref);
59 auto *LB = D->createLexicalBlock(unwrap<MDLocalScope>(Scope),
60 unwrap<MDFile>(File), Line, Column);
64 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
65 LLVMMetadataRef Scope,
67 unsigned Discriminator) {
68 DIBuilder *D = unwrap(Dref);
69 return wrap(D->createLexicalBlockFile(unwrap<MDLocalScope>(Scope),
70 unwrap<MDFile>(File), Discriminator));
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, LLVMValueRef Func) {
78 DIBuilder *D = unwrap(Dref);
79 return wrap(D->createFunction(unwrap<MDScope>(Scope), Name, LinkageName,
80 File ? unwrap<MDFile>(File) : nullptr, Line,
81 unwrap<MDSubroutineType>(CompositeType),
82 IsLocalToUnit, IsDefinition, ScopeLine, Flags,
83 IsOptimized, unwrap<Function>(Func)));
86 LLVMMetadataRef LLVMDIBuilderCreateLocalVariable(
87 LLVMDIBuilderRef Dref, unsigned Tag, LLVMMetadataRef Scope,
88 const char *Name, LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty,
89 int AlwaysPreserve, unsigned Flags, unsigned ArgNo) {
90 DIBuilder *D = unwrap(Dref);
91 DIVariable V = D->createLocalVariable(
92 Tag, unwrap<MDScope>(Scope), Name, unwrap<MDFile>(File), Line,
93 unwrap<MDType>(Ty), AlwaysPreserve, Flags, ArgNo);
97 LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
100 uint64_t AlignInBits,
102 DIBuilder *D = unwrap(Dref);
103 return wrap(D->createBasicType(Name, SizeInBits, AlignInBits, Encoding));
106 LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
107 LLVMMetadataRef PointeeType,
109 uint64_t AlignInBits,
111 DIBuilder *D = unwrap(Dref);
112 return wrap(D->createPointerType(unwrap<MDType>(PointeeType), SizeInBits,
117 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File,
118 LLVMMetadataRef ParameterTypes) {
119 DIBuilder *D = unwrap(Dref);
121 D->createSubroutineType(File ? unwrap<MDFile>(File) : nullptr,
122 DITypeArray(unwrap<MDTuple>(ParameterTypes))));
125 LLVMMetadataRef LLVMDIBuilderCreateStructType(
126 LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
127 LLVMMetadataRef File, unsigned Line, uint64_t SizeInBits,
128 uint64_t AlignInBits, unsigned Flags, LLVMMetadataRef DerivedFrom,
129 LLVMMetadataRef ElementTypes) {
130 DIBuilder *D = unwrap(Dref);
131 return wrap(D->createStructType(
132 unwrap<MDScope>(Scope), Name, File ? unwrap<MDFile>(File) : nullptr, Line,
133 SizeInBits, AlignInBits, Flags,
134 DerivedFrom ? unwrap<MDType>(DerivedFrom) : nullptr,
135 ElementTypes ? DIArray(unwrap<MDTuple>(ElementTypes)) : nullptr));
138 LLVMMetadataRef LLVMDIBuilderCreateReplaceableCompositeType(
139 LLVMDIBuilderRef Dref, unsigned Tag, const char *Name,
140 LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
141 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
143 DIBuilder *D = unwrap(Dref);
144 return wrap(D->createReplaceableCompositeType(
145 Tag, Name, unwrap<MDScope>(Scope), File ? unwrap<MDFile>(File) : nullptr,
146 Line, RuntimeLang, SizeInBits, AlignInBits, Flags));
150 LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef Dref, LLVMMetadataRef Scope,
151 const char *Name, LLVMMetadataRef File,
152 unsigned Line, uint64_t SizeInBits,
153 uint64_t AlignInBits, uint64_t OffsetInBits,
154 unsigned Flags, LLVMMetadataRef Ty) {
155 DIBuilder *D = unwrap(Dref);
156 return wrap(D->createMemberType(
157 unwrap<MDScope>(Scope), Name, File ? unwrap<MDFile>(File) : nullptr, Line,
158 SizeInBits, AlignInBits, OffsetInBits, Flags, unwrap<MDType>(Ty)));
161 LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
163 uint64_t AlignInBits,
164 LLVMMetadataRef ElementType,
165 LLVMMetadataRef Subscripts) {
166 DIBuilder *D = unwrap(Dref);
167 return wrap(D->createArrayType(SizeInBits, AlignInBits,
168 unwrap<MDType>(ElementType),
169 DIArray(unwrap<MDTuple>(Subscripts))));
172 LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,
173 LLVMMetadataRef Ty, const char *Name,
174 LLVMMetadataRef File, unsigned Line,
175 LLVMMetadataRef Context) {
176 DIBuilder *D = unwrap(Dref);
177 return wrap(D->createTypedef(unwrap<MDType>(Ty), Name,
178 File ? unwrap<MDFile>(File) : nullptr, Line,
179 Context ? unwrap<MDScope>(Context) : nullptr));
182 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
183 int64_t Lo, int64_t Count) {
184 DIBuilder *D = unwrap(Dref);
185 DISubrange S = D->getOrCreateSubrange(Lo, Count);
189 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
190 LLVMMetadataRef *Data,
192 DIBuilder *D = unwrap(Dref);
193 Metadata **DataValue = unwrap(Data);
194 ArrayRef<Metadata *> Elements(DataValue, Length);
195 DIArray A = D->getOrCreateArray(Elements);
196 return wrap(A.get());
199 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
200 LLVMMetadataRef *Data,
202 DIBuilder *D = unwrap(Dref);
203 Metadata **DataValue = unwrap(Data);
204 ArrayRef<Metadata *> Elements(DataValue, Length);
205 DITypeArray A = D->getOrCreateTypeArray(Elements);
206 return wrap(A.get());
209 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
210 int64_t *Addr, size_t Length) {
211 DIBuilder *D = unwrap(Dref);
212 DIExpression Expr = D->createExpression(ArrayRef<int64_t>(Addr, Length));
216 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
217 LLVMValueRef Storage,
218 LLVMMetadataRef VarInfo,
219 LLVMMetadataRef Expr,
220 LLVMBasicBlockRef Block) {
221 // Fail immediately here until the llgo folks update their bindings. The
222 // called function is going to assert out anyway.
223 llvm_unreachable("DIBuilder API change requires a DebugLoc");
225 DIBuilder *D = unwrap(Dref);
226 Instruction *Instr = D->insertDeclare(
227 unwrap(Storage), unwrap<MDLocalVariable>(VarInfo),
228 unwrap<MDExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
232 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
233 LLVMValueRef Val, uint64_t Offset,
234 LLVMMetadataRef VarInfo,
235 LLVMMetadataRef Expr,
236 LLVMBasicBlockRef Block) {
237 // Fail immediately here until the llgo folks update their bindings. The
238 // called function is going to assert out anyway.
239 llvm_unreachable("DIBuilder API change requires a DebugLoc");
241 DIBuilder *D = unwrap(Dref);
242 Instruction *Instr = D->insertDbgValueIntrinsic(
243 unwrap(Val), Offset, unwrap<MDLocalVariable>(VarInfo),
244 unwrap<MDExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));