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<DILocalScope>(Scope),
60 unwrap<DIFile>(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<DILocalScope>(Scope),
70 unwrap<DIFile>(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<DIScope>(Scope), Name, LinkageName,
80 File ? unwrap<DIFile>(File) : nullptr, Line,
81 unwrap<DISubroutineType>(CompositeType),
82 IsLocalToUnit, IsDefinition, ScopeLine, Flags,
83 IsOptimized, unwrap<Function>(Func)));
86 LLVMMetadataRef LLVMDIBuilderCreateLocalVariable(
87 LLVMDIBuilderRef Dref, unsigned, 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 // FIXME: Update the Go bindings to match the DIBuilder API.
93 return wrap(D->createParameterVariable(
94 unwrap<DIScope>(Scope), Name, ArgNo, unwrap<DIFile>(File), Line,
95 unwrap<DIType>(Ty), AlwaysPreserve, Flags));
96 return wrap(D->createAutoVariable(unwrap<DIScope>(Scope), Name,
97 unwrap<DIFile>(File), Line,
98 unwrap<DIType>(Ty), AlwaysPreserve, Flags));
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<DIType>(PointeeType), SizeInBits,
121 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File,
122 LLVMMetadataRef ParameterTypes) {
123 DIBuilder *D = unwrap(Dref);
125 D->createSubroutineType(File ? unwrap<DIFile>(File) : nullptr,
126 DITypeRefArray(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<DIScope>(Scope), Name, File ? unwrap<DIFile>(File) : nullptr, Line,
137 SizeInBits, AlignInBits, Flags,
138 DerivedFrom ? unwrap<DIType>(DerivedFrom) : nullptr,
139 ElementTypes ? DINodeArray(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<DIScope>(Scope), File ? unwrap<DIFile>(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<DIScope>(Scope), Name, File ? unwrap<DIFile>(File) : nullptr, Line,
162 SizeInBits, AlignInBits, OffsetInBits, Flags, unwrap<DIType>(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<DIType>(ElementType),
173 DINodeArray(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<DIType>(Ty), Name,
182 File ? unwrap<DIFile>(File) : nullptr, Line,
183 Context ? unwrap<DIScope>(Context) : nullptr));
186 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
187 int64_t Lo, int64_t Count) {
188 DIBuilder *D = unwrap(Dref);
189 return wrap(D->getOrCreateSubrange(Lo, Count));
192 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
193 LLVMMetadataRef *Data,
195 DIBuilder *D = unwrap(Dref);
196 Metadata **DataValue = unwrap(Data);
197 ArrayRef<Metadata *> Elements(DataValue, Length);
198 DINodeArray A = D->getOrCreateArray(Elements);
199 return wrap(A.get());
202 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
203 LLVMMetadataRef *Data,
205 DIBuilder *D = unwrap(Dref);
206 Metadata **DataValue = unwrap(Data);
207 ArrayRef<Metadata *> Elements(DataValue, Length);
208 DITypeRefArray A = D->getOrCreateTypeArray(Elements);
209 return wrap(A.get());
212 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
213 int64_t *Addr, size_t Length) {
214 DIBuilder *D = unwrap(Dref);
215 return wrap(D->createExpression(ArrayRef<int64_t>(Addr, Length)));
218 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
219 LLVMValueRef Storage,
220 LLVMMetadataRef VarInfo,
221 LLVMMetadataRef Expr,
222 LLVMBasicBlockRef Block) {
223 // Fail immediately here until the llgo folks update their bindings. The
224 // called function is going to assert out anyway.
225 llvm_unreachable("DIBuilder API change requires a DebugLoc");
227 DIBuilder *D = unwrap(Dref);
228 Instruction *Instr = D->insertDeclare(
229 unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
230 unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
234 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
235 LLVMValueRef Val, uint64_t Offset,
236 LLVMMetadataRef VarInfo,
237 LLVMMetadataRef Expr,
238 LLVMBasicBlockRef Block) {
239 // Fail immediately here until the llgo folks update their bindings. The
240 // called function is going to assert out anyway.
241 llvm_unreachable("DIBuilder API change requires a DebugLoc");
243 DIBuilder *D = unwrap(Dref);
244 Instruction *Instr = D->insertDbgValueIntrinsic(
245 unwrap(Val), Offset, unwrap<DILocalVariable>(VarInfo),
246 unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));