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"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/DIBuilder.h"
21 static Metadata *unwrapMetadata(LLVMValueRef VRef) {
22 Value *V = unwrap(VRef);
25 if (auto *MD = dyn_cast<MetadataAsValue>(V))
26 return MD->getMetadata();
27 return ValueAsMetadata::get(V);
30 static SmallVector<Metadata *, 8> unwrapMetadataArray(LLVMValueRef *Data,
32 SmallVector<Metadata *, 8> Elements;
33 for (size_t I = 0; I != Length; ++I)
34 Elements.push_back(unwrapMetadata(Data[I]));
39 template <typename T> T unwrapDI(LLVMValueRef v) {
40 return T(cast_or_null<MDNode>(unwrapMetadata(v)));
44 static LLVMValueRef wrapDI(DIDescriptor N) {
45 return wrap(MetadataAsValue::get(N->getContext(), N));
48 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
50 LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) {
51 Module *m = unwrap(mref);
52 return wrap(new DIBuilder(*m));
55 void LLVMDIBuilderDestroy(LLVMDIBuilderRef dref) {
56 DIBuilder *d = unwrap(dref);
60 void LLVMDIBuilderFinalize(LLVMDIBuilderRef dref) { unwrap(dref)->finalize(); }
62 LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
63 unsigned Lang, const char *File,
65 const char *Producer, int Optimized,
67 unsigned RuntimeVersion) {
68 DIBuilder *D = unwrap(Dref);
69 DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized,
70 Flags, RuntimeVersion);
74 LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
76 DIBuilder *D = unwrap(Dref);
77 DIFile F = D->createFile(File, Dir);
81 LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
83 LLVMValueRef File, unsigned Line,
85 DIBuilder *D = unwrap(Dref);
86 DILexicalBlock LB = D->createLexicalBlock(
87 unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Line, Column);
91 LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
94 unsigned Discriminator) {
95 DIBuilder *D = unwrap(Dref);
96 DILexicalBlockFile LBF = D->createLexicalBlockFile(
97 unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Discriminator);
101 LLVMValueRef LLVMDIBuilderCreateFunction(
102 LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
103 const char *LinkageName, LLVMValueRef File, unsigned Line,
104 LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition,
105 unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Func) {
106 DIBuilder *D = unwrap(Dref);
107 DISubprogram SP = D->createFunction(
108 unwrapDI<DIDescriptor>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
109 Line, unwrapDI<DICompositeType>(CompositeType), IsLocalToUnit,
110 IsDefinition, ScopeLine, Flags, IsOptimized, unwrap<Function>(Func));
114 LLVMValueRef LLVMDIBuilderCreateLocalVariable(
115 LLVMDIBuilderRef Dref, unsigned Tag, LLVMValueRef Scope, const char *Name,
116 LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve,
117 unsigned Flags, unsigned ArgNo) {
118 DIBuilder *D = unwrap(Dref);
119 DIVariable V = D->createLocalVariable(
120 Tag, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
121 unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, ArgNo);
125 LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
126 const char *Name, uint64_t SizeInBits,
127 uint64_t AlignInBits,
129 DIBuilder *D = unwrap(Dref);
130 DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding);
134 LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
135 LLVMValueRef PointeeType,
137 uint64_t AlignInBits,
139 DIBuilder *D = unwrap(Dref);
140 DIDerivedType T = D->createPointerType(unwrapDI<DIType>(PointeeType),
141 SizeInBits, AlignInBits, Name);
145 LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref,
147 LLVMValueRef ParameterTypes) {
148 DIBuilder *D = unwrap(Dref);
149 DICompositeType CT = D->createSubroutineType(
150 unwrapDI<DIFile>(File), unwrapDI<DITypeArray>(ParameterTypes));
154 LLVMValueRef LLVMDIBuilderCreateStructType(
155 LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
156 LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
157 unsigned Flags, LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes) {
158 DIBuilder *D = unwrap(Dref);
159 DICompositeType CT = D->createStructType(
160 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
161 SizeInBits, AlignInBits, Flags, unwrapDI<DIType>(DerivedFrom),
162 unwrapDI<DIArray>(ElementTypes));
166 LLVMValueRef LLVMDIBuilderCreateMemberType(
167 LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
168 LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
169 uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty) {
170 DIBuilder *D = unwrap(Dref);
171 DIDerivedType DT = D->createMemberType(
172 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
173 SizeInBits, AlignInBits, OffsetInBits, Flags, unwrapDI<DIType>(Ty));
177 LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
179 uint64_t AlignInBits,
180 LLVMValueRef ElementType,
181 LLVMValueRef Subscripts) {
182 DIBuilder *D = unwrap(Dref);
184 D->createArrayType(SizeInBits, AlignInBits, unwrapDI<DIType>(ElementType),
185 unwrapDI<DIArray>(Subscripts));
189 LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty,
190 const char *Name, LLVMValueRef File,
191 unsigned Line, LLVMValueRef Context) {
192 DIBuilder *D = unwrap(Dref);
194 D->createTypedef(unwrapDI<DIType>(Ty), Name, unwrapDI<DIFile>(File), Line,
195 unwrapDI<DIDescriptor>(Context));
199 LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, int64_t Lo,
201 DIBuilder *D = unwrap(Dref);
202 DISubrange S = D->getOrCreateSubrange(Lo, Count);
206 LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
207 LLVMValueRef *Data, size_t Length) {
208 DIBuilder *D = unwrap(Dref);
209 DIArray A = D->getOrCreateArray(unwrapMetadataArray(Data, Length));
213 LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
216 DIBuilder *D = unwrap(Dref);
217 DITypeArray A = D->getOrCreateTypeArray(unwrapMetadataArray(Data, Length));
221 LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr,
223 DIBuilder *D = unwrap(Dref);
224 DIExpression Expr = D->createExpression(ArrayRef<int64_t>(Addr, Length));
228 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
229 LLVMValueRef Storage,
230 LLVMValueRef VarInfo,
232 LLVMBasicBlockRef Block) {
233 DIBuilder *D = unwrap(Dref);
235 D->insertDeclare(unwrap(Storage), unwrapDI<DIVariable>(VarInfo),
236 unwrapDI<DIExpression>(Expr), unwrap(Block));
240 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
241 LLVMValueRef Val, uint64_t Offset,
242 LLVMValueRef VarInfo,
244 LLVMBasicBlockRef Block) {
245 DIBuilder *D = unwrap(Dref);
246 Instruction *Instr = D->insertDbgValueIntrinsic(
247 unwrap(Val), Offset, unwrapDI<DIVariable>(VarInfo),
248 unwrapDI<DIExpression>(Expr), unwrap(Block));