llvm.org GIT mirror llvm / eb8bf79
Go bindings: expose the Metadata type. Also modifies SetCurrentDebugLocation to take individual arguments rather than an MDNode. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224176 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 5 years ago
6 changed file(s) with 366 addition(s) and 294 deletion(s). Raw diff Collapse all Expand all
1212
1313 #include "DIBuilderBindings.h"
1414
15 #include "IRBindings.h"
16 #include "llvm/IR/IRBuilder.h"
1517 #include "llvm/IR/Module.h"
1618 #include "llvm/IR/DIBuilder.h"
1719
1820 using namespace llvm;
1921
20 static Metadata *unwrapMetadata(LLVMValueRef VRef) {
21 Value *V = unwrap(VRef);
22 if (!V)
23 return nullptr;
24 if (auto *MD = dyn_cast(V))
25 return MD->getMetadata();
26 return ValueAsMetadata::get(V);
27 }
28
29 static SmallVector unwrapMetadataArray(LLVMValueRef *Data,
30 size_t Length) {
31 SmallVector Elements;
32 for (size_t I = 0; I != Length; ++I)
33 Elements.push_back(unwrapMetadata(Data[I]));
34 return Elements;
35 }
22 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
3623
3724 namespace {
38 template T unwrapDI(LLVMValueRef v) {
39 return T(cast_or_null(unwrapMetadata(v)));
40 }
41 }
42
43 static LLVMValueRef wrapDI(DIDescriptor N) {
44 return wrap(MetadataAsValue::get(N->getContext(), N));
45 }
46
47 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
25 template T unwrapDI(LLVMMetadataRef v) {
26 return v ? T(unwrap(v)) : T();
27 }
28 }
4829
4930 LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) {
5031 Module *m = unwrap(mref);
5839
5940 void LLVMDIBuilderFinalize(LLVMDIBuilderRef dref) { unwrap(dref)->finalize(); }
6041
61 LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
62 unsigned Lang, const char *File,
63 const char *Dir,
64 const char *Producer, int Optimized,
65 const char *Flags,
66 unsigned RuntimeVersion) {
42 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
43 unsigned Lang, const char *File,
44 const char *Dir,
45 const char *Producer,
46 int Optimized, const char *Flags,
47 unsigned RuntimeVersion) {
6748 DIBuilder *D = unwrap(Dref);
6849 DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized,
6950 Flags, RuntimeVersion);
70 return wrapDI(CU);
71 }
72
73 LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
74 const char *Dir) {
51 return wrap(CU);
52 }
53
54 LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
55 const char *Dir) {
7556 DIBuilder *D = unwrap(Dref);
7657 DIFile F = D->createFile(File, Dir);
77 return wrapDI(F);
78 }
79
80 LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
81 LLVMValueRef Scope,
82 LLVMValueRef File, unsigned Line,
83 unsigned Column) {
58 return wrap(F);
59 }
60
61 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
62 LLVMMetadataRef Scope,
63 LLVMMetadataRef File,
64 unsigned Line,
65 unsigned Column) {
8466 DIBuilder *D = unwrap(Dref);
8567 DILexicalBlock LB = D->createLexicalBlock(
8668 unwrapDI(Scope), unwrapDI(File), Line, Column);
87 return wrapDI(LB);
88 }
89
90 LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
91 LLVMValueRef Scope,
92 LLVMValueRef File,
93 unsigned Discriminator) {
69 return wrap(LB);
70 }
71
72 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
73 LLVMMetadataRef Scope,
74 LLVMMetadataRef File,
75 unsigned Discriminator) {
9476 DIBuilder *D = unwrap(Dref);
9577 DILexicalBlockFile LBF = D->createLexicalBlockFile(
9678 unwrapDI(Scope), unwrapDI(File), Discriminator);
97 return wrapDI(LBF);
98 }
99
100 LLVMValueRef LLVMDIBuilderCreateFunction(
101 LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
102 const char *LinkageName, LLVMValueRef File, unsigned Line,
103 LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition,
79 return wrap(LBF);
80 }
81
82 LLVMMetadataRef LLVMDIBuilderCreateFunction(
83 LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
84 const char *LinkageName, LLVMMetadataRef File, unsigned Line,
85 LLVMMetadataRef CompositeType, int IsLocalToUnit, int IsDefinition,
10486 unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Func) {
10587 DIBuilder *D = unwrap(Dref);
10688 DISubprogram SP = D->createFunction(
10789 unwrapDI(Scope), Name, LinkageName, unwrapDI(File),
10890 Line, unwrapDI(CompositeType), IsLocalToUnit,
10991 IsDefinition, ScopeLine, Flags, IsOptimized, unwrap(Func));
110 return wrapDI(SP);
111 }
112
113 LLVMValueRef LLVMDIBuilderCreateLocalVariable(
114 LLVMDIBuilderRef Dref, unsigned Tag, LLVMValueRef Scope, const char *Name,
115 LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve,
116 unsigned Flags, unsigned ArgNo) {
92 return wrap(SP);
93 }
94
95 LLVMMetadataRef LLVMDIBuilderCreateLocalVariable(
96 LLVMDIBuilderRef Dref, unsigned Tag, LLVMMetadataRef Scope,
97 const char *Name, LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty,
98 int AlwaysPreserve, unsigned Flags, unsigned ArgNo) {
11799 DIBuilder *D = unwrap(Dref);
118100 DIVariable V = D->createLocalVariable(
119101 Tag, unwrapDI(Scope), Name, unwrapDI(File), Line,
120102 unwrapDI(Ty), AlwaysPreserve, Flags, ArgNo);
121 return wrapDI(V);
122 }
123
124 LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
125 const char *Name, uint64_t SizeInBits,
126 uint64_t AlignInBits,
127 unsigned Encoding) {
103 return wrap(V);
104 }
105
106 LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
107 const char *Name,
108 uint64_t SizeInBits,
109 uint64_t AlignInBits,
110 unsigned Encoding) {
128111 DIBuilder *D = unwrap(Dref);
129112 DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding);
130 return wrapDI(T);
131 }
132
133 LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
134 LLVMValueRef PointeeType,
135 uint64_t SizeInBits,
136 uint64_t AlignInBits,
137 const char *Name) {
113 return wrap(T);
114 }
115
116 LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
117 LLVMMetadataRef PointeeType,
118 uint64_t SizeInBits,
119 uint64_t AlignInBits,
120 const char *Name) {
138121 DIBuilder *D = unwrap(Dref);
139122 DIDerivedType T = D->createPointerType(unwrapDI(PointeeType),
140123 SizeInBits, AlignInBits, Name);
141 return wrapDI(T);
142 }
143
144 LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref,
145 LLVMValueRef File,
146 LLVMValueRef ParameterTypes) {
124 return wrap(T);
125 }
126
127 LLVMMetadataRef
128 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File,
129 LLVMMetadataRef ParameterTypes) {
147130 DIBuilder *D = unwrap(Dref);
148131 DICompositeType CT = D->createSubroutineType(
149132 unwrapDI(File), unwrapDI(ParameterTypes));
150 return wrapDI(CT);
151 }
152
153 LLVMValueRef LLVMDIBuilderCreateStructType(
154 LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
155 LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
156 unsigned Flags, LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes) {
133 return wrap(CT);
134 }
135
136 LLVMMetadataRef LLVMDIBuilderCreateStructType(
137 LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
138 LLVMMetadataRef File, unsigned Line, uint64_t SizeInBits,
139 uint64_t AlignInBits, unsigned Flags, LLVMMetadataRef DerivedFrom,
140 LLVMMetadataRef ElementTypes) {
157141 DIBuilder *D = unwrap(Dref);
158142 DICompositeType CT = D->createStructType(
159143 unwrapDI(Scope), Name, unwrapDI(File), Line,
160144 SizeInBits, AlignInBits, Flags, unwrapDI(DerivedFrom),
161145 unwrapDI(ElementTypes));
162 return wrapDI(CT);
163 }
164
165 LLVMValueRef LLVMDIBuilderCreateMemberType(
166 LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
167 LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
168 uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty) {
146 return wrap(CT);
147 }
148
149 LLVMMetadataRef
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) {
169155 DIBuilder *D = unwrap(Dref);
170156 DIDerivedType DT = D->createMemberType(
171157 unwrapDI(Scope), Name, unwrapDI(File), Line,
172158 SizeInBits, AlignInBits, OffsetInBits, Flags, unwrapDI(Ty));
173 return wrapDI(DT);
174 }
175
176 LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
177 uint64_t SizeInBits,
178 uint64_t AlignInBits,
179 LLVMValueRef ElementType,
180 LLVMValueRef Subscripts) {
159 return wrap(DT);
160 }
161
162 LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
163 uint64_t SizeInBits,
164 uint64_t AlignInBits,
165 LLVMMetadataRef ElementType,
166 LLVMMetadataRef Subscripts) {
181167 DIBuilder *D = unwrap(Dref);
182168 DICompositeType CT =
183169 D->createArrayType(SizeInBits, AlignInBits, unwrapDI(ElementType),
184170 unwrapDI(Subscripts));
185 return wrapDI(CT);
186 }
187
188 LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty,
189 const char *Name, LLVMValueRef File,
190 unsigned Line, LLVMValueRef Context) {
171 return wrap(CT);
172 }
173
174 LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,
175 LLVMMetadataRef Ty, const char *Name,
176 LLVMMetadataRef File, unsigned Line,
177 LLVMMetadataRef Context) {
191178 DIBuilder *D = unwrap(Dref);
192179 DIDerivedType DT =
193180 D->createTypedef(unwrapDI(Ty), Name, unwrapDI(File), Line,
194181 unwrapDI(Context));
195 return wrapDI(DT);
196 }
197
198 LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, int64_t Lo,
199 int64_t Count) {
182 return wrap(DT);
183 }
184
185 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
186 int64_t Lo, int64_t Count) {
200187 DIBuilder *D = unwrap(Dref);
201188 DISubrange S = D->getOrCreateSubrange(Lo, Count);
202 return wrapDI(S);
203 }
204
205 LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
206 LLVMValueRef *Data, size_t Length) {
207 DIBuilder *D = unwrap(Dref);
208 DIArray A = D->getOrCreateArray(unwrapMetadataArray(Data, Length));
209 return wrapDI(A);
210 }
211
212 LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
213 LLVMValueRef *Data,
214 size_t Length) {
215 DIBuilder *D = unwrap(Dref);
216 DITypeArray A = D->getOrCreateTypeArray(unwrapMetadataArray(Data, Length));
217 return wrapDI(A);
218 }
219
220 LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr,
221 size_t Length) {
189 return wrap(S);
190 }
191
192 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
193 LLVMMetadataRef *Data,
194 size_t Length) {
195 DIBuilder *D = unwrap(Dref);
196 Metadata **DataValue = unwrap(Data);
197 ArrayRef Elements(DataValue, Length);
198 DIArray A = D->getOrCreateArray(Elements);
199 return wrap(A);
200 }
201
202 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
203 LLVMMetadataRef *Data,
204 size_t Length) {
205 DIBuilder *D = unwrap(Dref);
206 Metadata **DataValue = unwrap(Data);
207 ArrayRef Elements(DataValue, Length);
208 DITypeArray A = D->getOrCreateTypeArray(Elements);
209 return wrap(A);
210 }
211
212 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
213 int64_t *Addr, size_t Length) {
222214 DIBuilder *D = unwrap(Dref);
223215 DIExpression Expr = D->createExpression(ArrayRef(Addr, Length));
224 return wrapDI(Expr);
216 return wrap(Expr);
225217 }
226218
227219 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
228220 LLVMValueRef Storage,
229 LLVMValueRef VarInfo,
230 LLVMValueRef Expr,
221 LLVMMetadataRef VarInfo,
222 LLVMMetadataRef Expr,
231223 LLVMBasicBlockRef Block) {
232224 DIBuilder *D = unwrap(Dref);
233225 Instruction *Instr =
238230
239231 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
240232 LLVMValueRef Val, uint64_t Offset,
241 LLVMValueRef VarInfo,
242 LLVMValueRef Expr,
233 LLVMMetadataRef VarInfo,
234 LLVMMetadataRef Expr,
243235 LLVMBasicBlockRef Block) {
244236 DIBuilder *D = unwrap(Dref);
245237 Instruction *Instr = D->insertDbgValueIntrinsic(
1414 #define LLVM_BINDINGS_GO_LLVM_DIBUILDERBINDINGS_H
1515
1616 #include "llvm-c/Core.h"
17 #include "IRBindings.h"
1718
1819 #ifdef __cplusplus
1920 extern "C" {
3031 void LLVMDIBuilderDestroy(LLVMDIBuilderRef d);
3132 void LLVMDIBuilderFinalize(LLVMDIBuilderRef d);
3233
33 LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef D,
34 unsigned Language, const char *File,
35 const char *Dir,
36 const char *Producer, int Optimized,
37 const char *Flags,
38 unsigned RuntimeVersion);
34 LLVMMetadataRef
35 LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef D, unsigned Language,
36 const char *File, const char *Dir,
37 const char *Producer, int Optimized,
38 const char *Flags, unsigned RuntimeVersion);
3939
40 LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef D, const char *File,
41 const char *Dir);
40 LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef D, const char *File,
41 const char *Dir);
4242
43 LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef D,
44 LLVMValueRef Scope,
45 LLVMValueRef File, unsigned Line,
46 unsigned Column);
43 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef D,
44 LLVMMetadataRef Scope,
45 LLVMMetadataRef File,
46 unsigned Line, unsigned Column);
4747
48 LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef D,
49 LLVMValueRef Scope,
50 LLVMValueRef File,
51 unsigned Discriminator);
48 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef D,
49 LLVMMetadataRef Scope,
50 LLVMMetadataRef File,
51 unsigned Discriminator);
5252
53 LLVMValueRef LLVMDIBuilderCreateFunction(
54 LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name,
55 const char *LinkageName, LLVMValueRef File, unsigned Line,
56 LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition,
53 LLVMMetadataRef LLVMDIBuilderCreateFunction(
54 LLVMDIBuilderRef D, LLVMMetadataRef Scope, const char *Name,
55 const char *LinkageName, LLVMMetadataRef File, unsigned Line,
56 LLVMMetadataRef CompositeType, int IsLocalToUnit, int IsDefinition,
5757 unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Function);
5858
59 LLVMValueRef LLVMDIBuilderCreateLocalVariable(
60 LLVMDIBuilderRef D, unsigned Tag, LLVMValueRef Scope, const char *Name,
61 LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve,
59 LLVMMetadataRef LLVMDIBuilderCreateLocalVariable(
60 LLVMDIBuilderRef D, unsigned Tag, LLVMMetadataRef Scope, const char *Name,
61 LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty, int AlwaysPreserve,
6262 unsigned Flags, unsigned ArgNo);
6363
64 LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef D, const char *Name,
65 uint64_t SizeInBits,
66 uint64_t AlignInBits,
67 unsigned Encoding);
64 LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef D,
65 const char *Name,
66 uint64_t SizeInBits,
67 uint64_t AlignInBits,
68 unsigned Encoding);
6869
69 LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef D,
70 LLVMValueRef PointeeType,
71 uint64_t SizeInBits,
72 uint64_t AlignInBits,
73 const char *Name);
70 LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef D,
71 LLVMMetadataRef PointeeType,
72 uint64_t SizeInBits,
73 uint64_t AlignInBits,
74 const char *Name);
7475
75 LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef D,
76 LLVMValueRef File,
77 LLVMValueRef ParameterTypes);
76 LLVMMetadataRef
77 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef D, LLVMMetadataRef File,
78 LLVMMetadataRef ParameterTypes);
7879
79 LLVMValueRef LLVMDIBuilderCreateStructType(
80 LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name, LLVMValueRef File,
81 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
82 LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes);
80 LLVMMetadataRef LLVMDIBuilderCreateStructType(
81 LLVMDIBuilderRef D, LLVMMetadataRef Scope, const char *Name,
82 LLVMMetadataRef File, unsigned Line, uint64_t SizeInBits,
83 uint64_t AlignInBits, unsigned Flags, LLVMMetadataRef DerivedFrom,
84 LLVMMetadataRef ElementTypes);
8385
84 LLVMValueRef LLVMDIBuilderCreateMemberType(
85 LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name, LLVMValueRef File,
86 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
87 uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty);
86 LLVMMetadataRef
87 LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef D, LLVMMetadataRef Scope,
88 const char *Name, LLVMMetadataRef File,
89 unsigned Line, uint64_t SizeInBits,
90 uint64_t AlignInBits, uint64_t OffsetInBits,
91 unsigned Flags, LLVMMetadataRef Ty);
8892
89 LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef D,
90 uint64_t SizeInBits,
91 uint64_t AlignInBits,
92 LLVMValueRef ElementType,
93 LLVMValueRef Subscripts);
93 LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef D,
94 uint64_t SizeInBits,
95 uint64_t AlignInBits,
96 LLVMMetadataRef ElementType,
97 LLVMMetadataRef Subscripts);
9498
95 LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef D, LLVMValueRef Ty,
96 const char *Name, LLVMValueRef File,
97 unsigned Line, LLVMValueRef Context);
99 LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef D,
100 LLVMMetadataRef Ty, const char *Name,
101 LLVMMetadataRef File, unsigned Line,
102 LLVMMetadataRef Context);
98103
99 LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef D, int64_t Lo,
100 int64_t Count);
104 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef D, int64_t Lo,
105 int64_t Count);
101106
102 LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef D,
103 LLVMValueRef *Data, size_t Length);
107 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef D,
108 LLVMMetadataRef *Data,
109 size_t Length);
104110
105 LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef D,
106 LLVMValueRef *Data,
107 size_t Length);
111 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef D,
112 LLVMMetadataRef *Data,
113 size_t Length);
108114
109 LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr,
110 size_t Length);
115 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
116 int64_t *Addr, size_t Length);
111117
112118 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef D,
113119 LLVMValueRef Storage,
114 LLVMValueRef VarInfo,
115 LLVMValueRef Expr,
120 LLVMMetadataRef VarInfo,
121 LLVMMetadataRef Expr,
116122 LLVMBasicBlockRef Block);
117123
118 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef D,
119 LLVMValueRef Val,
124 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef D, LLVMValueRef Val,
120125 uint64_t Offset,
121 LLVMValueRef VarInfo,
122 LLVMValueRef Expr,
126 LLVMMetadataRef VarInfo,
127 LLVMMetadataRef Expr,
123128 LLVMBasicBlockRef Block);
124129
125130 #ifdef __cplusplus
126 } // extern "C"
131 } // extern "C"
127132 #endif
128133
129134 #endif
1313 #include "IRBindings.h"
1414
1515 #include "llvm/IR/Attributes.h"
16 #include "llvm/IR/DebugLoc.h"
1617 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Module.h"
1721
1822 using namespace llvm;
1923
4448 AttributeSet::FunctionIndex, B));
4549 Func->setAttributes(PALnew);
4650 }
51
52 LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
53 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
54 }
55
56 LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
57 unsigned Count) {
58 return wrap(
59 MDNode::get(*unwrap(C), ArrayRef(unwrap(MDs), Count)));
60 }
61
62 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
63 LLVMMetadataRef Val) {
64 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
65 if (!N)
66 return;
67 if (!Val)
68 return;
69 N->addOperand(unwrap(Val));
70 }
71
72 void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
73 MDNode *N = MD ? unwrap(MD) : nullptr;
74 unwrap(Inst)->setMetadata(KindID, N);
75 }
76
77 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
78 unsigned Col, LLVMMetadataRef Scope,
79 LLVMMetadataRef InlinedAt) {
80 unwrap(Bref)->SetCurrentDebugLocation(
81 DebugLoc::get(Line, Col, Scope ? unwrap(Scope) : nullptr,
82 InlinedAt ? unwrap(InlinedAt) : nullptr));
83 }
1414 #define LLVM_BINDINGS_GO_LLVM_IRBINDINGS_H
1515
1616 #include "llvm-c/Core.h"
17 #ifdef __cplusplus
18 #include "llvm/IR/Metadata.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #endif
21
1722 #include
1823
1924 #ifdef __cplusplus
2025 extern "C" {
2126 #endif
27
28 typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;
2229
2330 // These functions duplicate the LLVM*FunctionAttr functions in the stable C
2431 // API. We cannot use the existing functions because they take 32-bit attribute
2936 uint64_t LLVMGetFunctionAttr2(LLVMValueRef Fn);
3037 void LLVMRemoveFunctionAttr2(LLVMValueRef Fn, uint64_t PA);
3138
39 LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen);
40 LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
41 unsigned Count);
42
43 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
44 LLVMMetadataRef Val);
45 void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD);
46
47 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
48 unsigned Col, LLVMMetadataRef Scope,
49 LLVMMetadataRef InlinedAt);
50
3251 #ifdef __cplusplus
3352 }
53
54 namespace llvm {
55
56 DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)
57
58 inline Metadata **unwrap(LLVMMetadataRef *Vals) {
59 return reinterpret_cast(Vals);
60 }
61
62 }
63
3464 #endif
3565
3666 #endif
120120 }
121121
122122 // CreateCompileUnit creates compile unit debug metadata.
123 func (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Value {
123 func (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Metadata {
124124 file := C.CString(cu.File)
125125 defer C.free(unsafe.Pointer(file))
126126 dir := C.CString(cu.Dir)
138138 flags,
139139 C.unsigned(cu.RuntimeVersion),
140140 )
141 return Value{C: result}
141 return Metadata{C: result}
142142 }
143143
144144 // CreateCompileUnit creates file debug metadata.
145 func (d *DIBuilder) CreateFile(filename, dir string) Value {
145 func (d *DIBuilder) CreateFile(filename, dir string) Metadata {
146146 cfilename := C.CString(filename)
147147 defer C.free(unsafe.Pointer(cfilename))
148148 cdir := C.CString(dir)
149149 defer C.free(unsafe.Pointer(cdir))
150150 result := C.LLVMDIBuilderCreateFile(d.ref, cfilename, cdir)
151 return Value{C: result}
151 return Metadata{C: result}
152152 }
153153
154154 // DILexicalBlock holds the values for creating lexical block debug metadata.
155155 type DILexicalBlock struct {
156 File Value
156 File Metadata
157157 Line int
158158 Column int
159159 }
160160
161161 // CreateCompileUnit creates lexical block debug metadata.
162 func (d *DIBuilder) CreateLexicalBlock(diScope Value, b DILexicalBlock) Value {
162 func (d *DIBuilder) CreateLexicalBlock(diScope Metadata, b DILexicalBlock) Metadata {
163163 result := C.LLVMDIBuilderCreateLexicalBlock(
164164 d.ref,
165165 diScope.C,
167167 C.unsigned(b.Line),
168168 C.unsigned(b.Column),
169169 )
170 return Value{C: result}
171 }
172
173 func (d *DIBuilder) CreateLexicalBlockFile(diScope Value, diFile Value, discriminator int) Value {
170 return Metadata{C: result}
171 }
172
173 func (d *DIBuilder) CreateLexicalBlockFile(diScope Metadata, diFile Metadata, discriminator int) Metadata {
174174 result := C.LLVMDIBuilderCreateLexicalBlockFile(d.ref, diScope.C, diFile.C,
175175 C.unsigned(discriminator))
176 return Value{C: result}
176 return Metadata{C: result}
177177 }
178178
179179 // DIFunction holds the values for creating function debug metadata.
180180 type DIFunction struct {
181181 Name string
182182 LinkageName string
183 File Value
183 File Metadata
184184 Line int
185 Type Value
185 Type Metadata
186186 LocalToUnit bool
187187 IsDefinition bool
188188 ScopeLine int
192192 }
193193
194194 // CreateCompileUnit creates function debug metadata.
195 func (d *DIBuilder) CreateFunction(diScope Value, f DIFunction) Value {
195 func (d *DIBuilder) CreateFunction(diScope Metadata, f DIFunction) Metadata {
196196 name := C.CString(f.Name)
197197 defer C.free(unsafe.Pointer(name))
198198 linkageName := C.CString(f.LinkageName)
212212 boolToCInt(f.Optimized),
213213 f.Function.C,
214214 )
215 return Value{C: result}
215 return Metadata{C: result}
216216 }
217217
218218 // DILocalVariable holds the values for creating local variable debug metadata.
219219 type DILocalVariable struct {
220220 Tag dwarf.Tag
221221 Name string
222 File Value
222 File Metadata
223223 Line int
224 Type Value
224 Type Metadata
225225 AlwaysPreserve bool
226226 Flags int
227227
231231 }
232232
233233 // CreateLocalVariable creates local variable debug metadata.
234 func (d *DIBuilder) CreateLocalVariable(scope Value, v DILocalVariable) Value {
234 func (d *DIBuilder) CreateLocalVariable(scope Metadata, v DILocalVariable) Metadata {
235235 name := C.CString(v.Name)
236236 defer C.free(unsafe.Pointer(name))
237237 result := C.LLVMDIBuilderCreateLocalVariable(
246246 C.unsigned(v.Flags),
247247 C.unsigned(v.ArgNo),
248248 )
249 return Value{C: result}
249 return Metadata{C: result}
250250 }
251251
252252 // DIBasicType holds the values for creating basic type debug metadata.
258258 }
259259
260260 // CreateBasicType creates basic type debug metadata.
261 func (d *DIBuilder) CreateBasicType(t DIBasicType) Value {
261 func (d *DIBuilder) CreateBasicType(t DIBasicType) Metadata {
262262 name := C.CString(t.Name)
263263 defer C.free(unsafe.Pointer(name))
264264 result := C.LLVMDIBuilderCreateBasicType(
268268 C.uint64_t(t.AlignInBits),
269269 C.unsigned(t.Encoding),
270270 )
271 return Value{C: result}
271 return Metadata{C: result}
272272 }
273273
274274 // DIPointerType holds the values for creating pointer type debug metadata.
275275 type DIPointerType struct {
276 Pointee Value
276 Pointee Metadata
277277 SizeInBits uint64
278278 AlignInBits uint64 // optional
279279 Name string // optional
280280 }
281281
282282 // CreateBasicType creates basic type debug metadata.
283 func (d *DIBuilder) CreatePointerType(t DIPointerType) Value {
283 func (d *DIBuilder) CreatePointerType(t DIPointerType) Metadata {
284284 name := C.CString(t.Name)
285285 defer C.free(unsafe.Pointer(name))
286286 result := C.LLVMDIBuilderCreatePointerType(
290290 C.uint64_t(t.AlignInBits),
291291 name,
292292 )
293 return Value{C: result}
293 return Metadata{C: result}
294294 }
295295
296296 // DISubroutineType holds the values for creating subroutine type debug metadata.
297297 type DISubroutineType struct {
298298 // File is the file in which the subroutine type is defined.
299 File Value
299 File Metadata
300300
301301 // Parameters contains the subroutine parameter types,
302302 // including the return type at the 0th index.
303 Parameters []Value
303 Parameters []Metadata
304304 }
305305
306306 // CreateSubroutineType creates subroutine type debug metadata.
307 func (d *DIBuilder) CreateSubroutineType(t DISubroutineType) Value {
307 func (d *DIBuilder) CreateSubroutineType(t DISubroutineType) Metadata {
308308 params := d.getOrCreateTypeArray(t.Parameters)
309309 result := C.LLVMDIBuilderCreateSubroutineType(d.ref, t.File.C, params.C)
310 return Value{C: result}
310 return Metadata{C: result}
311311 }
312312
313313 // DIStructType holds the values for creating struct type debug metadata.
314314 type DIStructType struct {
315315 Name string
316 File Value
316 File Metadata
317317 Line int
318318 SizeInBits uint64
319319 AlignInBits uint64
320320 Flags int
321 DerivedFrom Value
322 Elements []Value
321 DerivedFrom Metadata
322 Elements []Metadata
323323 }
324324
325325 // CreateStructType creates struct type debug metadata.
326 func (d *DIBuilder) CreateStructType(scope Value, t DIStructType) Value {
326 func (d *DIBuilder) CreateStructType(scope Metadata, t DIStructType) Metadata {
327327 elements := d.getOrCreateArray(t.Elements)
328328 name := C.CString(t.Name)
329329 defer C.free(unsafe.Pointer(name))
339339 t.DerivedFrom.C,
340340 elements.C,
341341 )
342 return Value{C: result}
342 return Metadata{C: result}
343343 }
344344
345345 // DIMemberType holds the values for creating member type debug metadata.
346346 type DIMemberType struct {
347347 Name string
348 File Value
348 File Metadata
349349 Line int
350350 SizeInBits uint64
351351 AlignInBits uint64
352352 OffsetInBits uint64
353353 Flags int
354 Type Value
354 Type Metadata
355355 }
356356
357357 // CreateMemberType creates struct type debug metadata.
358 func (d *DIBuilder) CreateMemberType(scope Value, t DIMemberType) Value {
358 func (d *DIBuilder) CreateMemberType(scope Metadata, t DIMemberType) Metadata {
359359 name := C.CString(t.Name)
360360 defer C.free(unsafe.Pointer(name))
361361 result := C.LLVMDIBuilderCreateMemberType(
370370 C.unsigned(t.Flags),
371371 t.Type.C,
372372 )
373 return Value{C: result}
373 return Metadata{C: result}
374374 }
375375
376376 // DISubrange describes an integer value range.
383383 type DIArrayType struct {
384384 SizeInBits uint64
385385 AlignInBits uint64
386 ElementType Value
386 ElementType Metadata
387387 Subscripts []DISubrange
388388 }
389389
390390 // CreateArrayType creates struct type debug metadata.
391 func (d *DIBuilder) CreateArrayType(t DIArrayType) Value {
392 subscriptsSlice := make([]Value, len(t.Subscripts))
391 func (d *DIBuilder) CreateArrayType(t DIArrayType) Metadata {
392 subscriptsSlice := make([]Metadata, len(t.Subscripts))
393393 for i, s := range t.Subscripts {
394394 subscriptsSlice[i] = d.getOrCreateSubrange(s.Lo, s.Count)
395395 }
401401 t.ElementType.C,
402402 subscripts.C,
403403 )
404 return Value{C: result}
404 return Metadata{C: result}
405405 }
406406
407407 // DITypedef holds the values for creating typedef type debug metadata.
408408 type DITypedef struct {
409 Type Value
409 Type Metadata
410410 Name string
411 File Value
411 File Metadata
412412 Line int
413 Context Value
413 Context Metadata
414414 }
415415
416416 // CreateTypedef creates typedef type debug metadata.
417 func (d *DIBuilder) CreateTypedef(t DITypedef) Value {
417 func (d *DIBuilder) CreateTypedef(t DITypedef) Metadata {
418418 name := C.CString(t.Name)
419419 defer C.free(unsafe.Pointer(name))
420420 result := C.LLVMDIBuilderCreateTypedef(
425425 C.unsigned(t.Line),
426426 t.Context.C,
427427 )
428 return Value{C: result}
428 return Metadata{C: result}
429429 }
430430
431431 // getOrCreateSubrange gets a metadata node for the specified subrange,
432432 // creating if required.
433 func (d *DIBuilder) getOrCreateSubrange(lo, count int64) Value {
433 func (d *DIBuilder) getOrCreateSubrange(lo, count int64) Metadata {
434434 result := C.LLVMDIBuilderGetOrCreateSubrange(d.ref, C.int64_t(lo), C.int64_t(count))
435 return Value{C: result}
435 return Metadata{C: result}
436436 }
437437
438438 // getOrCreateArray gets a metadata node containing the specified values,
439439 // creating if required.
440 func (d *DIBuilder) getOrCreateArray(values []Value) Value {
440 func (d *DIBuilder) getOrCreateArray(values []Metadata) Metadata {
441441 if len(values) == 0 {
442 return Value{}
442 return Metadata{}
443443 }
444 var data *C.LLVMValueRef
445 length := len(values)
446 if length > 0 {
447 data = &values[0].C
448 }
444 data, length := llvmMetadataRefs(values)
449445 result := C.LLVMDIBuilderGetOrCreateArray(d.ref, data, C.size_t(length))
450 return Value{C: result}
446 return Metadata{C: result}
451447 }
452448
453449 // getOrCreateTypeArray gets a metadata node for a type array containing the
454450 // specified values, creating if required.
455 func (d *DIBuilder) getOrCreateTypeArray(values []Value) Value {
451 func (d *DIBuilder) getOrCreateTypeArray(values []Metadata) Metadata {
456452 if len(values) == 0 {
457 return Value{}
453 return Metadata{}
458454 }
459 var data *C.LLVMValueRef
460 length := len(values)
461 if length > 0 {
462 data = &values[0].C
463 }
455 data, length := llvmMetadataRefs(values)
464456 result := C.LLVMDIBuilderGetOrCreateTypeArray(d.ref, data, C.size_t(length))
465 return Value{C: result}
457 return Metadata{C: result}
466458 }
467459
468460 // CreateExpression creates a new descriptor for the specified
469461 // variable which has a complex address expression for its address.
470 func (d *DIBuilder) CreateExpression(addr []int64) Value {
462 func (d *DIBuilder) CreateExpression(addr []int64) Metadata {
471463 var data *C.int64_t
472464 if len(addr) > 0 {
473465 data = (*C.int64_t)(unsafe.Pointer(&addr[0]))
474466 }
475467 result := C.LLVMDIBuilderCreateExpression(d.ref, data, C.size_t(len(addr)))
476 return Value{C: result}
468 return Metadata{C: result}
477469 }
478470
479471 // InsertDeclareAtEnd inserts a call to llvm.dbg.declare at the end of the
480472 // specified basic block for the given value and associated debug metadata.
481 func (d *DIBuilder) InsertDeclareAtEnd(v, diVarInfo, expr Value, bb BasicBlock) Value {
473 func (d *DIBuilder) InsertDeclareAtEnd(v Value, diVarInfo, expr Metadata, bb BasicBlock) Value {
482474 result := C.LLVMDIBuilderInsertDeclareAtEnd(d.ref, v.C, diVarInfo.C, expr.C, bb.C)
483475 return Value{C: result}
484476 }
485477
486478 // InsertValueAtEnd inserts a call to llvm.dbg.value at the end of the
487479 // specified basic block for the given value and associated debug metadata.
488 func (d *DIBuilder) InsertValueAtEnd(v, diVarInfo, expr Value, offset uint64, bb BasicBlock) Value {
480 func (d *DIBuilder) InsertValueAtEnd(v Value, diVarInfo, expr Metadata, offset uint64, bb BasicBlock) Value {
489481 result := C.LLVMDIBuilderInsertValueAtEnd(d.ref, v.C, C.uint64_t(offset), diVarInfo.C, expr.C, bb.C)
490482 return Value{C: result}
491483 }
5454 Use struct {
5555 C C.LLVMUseRef
5656 }
57 Metadata struct {
58 C C.LLVMMetadataRef
59 }
5760 Attribute uint64
5861 Opcode C.LLVMOpcode
5962 TypeKind C.LLVMTypeKind
7982 // helpers
8083 func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) }
8184 func llvmValueRefPtr(t *Value) *C.LLVMValueRef { return (*C.LLVMValueRef)(unsafe.Pointer(t)) }
85 func llvmMetadataRefPtr(t *Metadata) *C.LLVMMetadataRef {
86 return (*C.LLVMMetadataRef)(unsafe.Pointer(t))
87 }
8288 func llvmBasicBlockRefPtr(t *BasicBlock) *C.LLVMBasicBlockRef {
8389 return (*C.LLVMBasicBlockRef)(unsafe.Pointer(t))
8490 }
94100 ptlen := C.unsigned(len(values))
95101 if ptlen > 0 {
96102 pt = llvmValueRefPtr(&values[0])
103 }
104 return pt, ptlen
105 }
106
107 func llvmMetadataRefs(mds []Metadata) (*C.LLVMMetadataRef, C.unsigned) {
108 var pt *C.LLVMMetadataRef
109 ptlen := C.unsigned(len(mds))
110 if ptlen > 0 {
111 pt = llvmMetadataRefPtr(&mds[0])
97112 }
98113 return pt, ptlen
99114 }
420435 C.LLVMSetModuleInlineAsm(m.C, casm)
421436 }
422437
423 func (m Module) AddNamedMetadataOperand(name string, operand Value) {
424 cname := C.CString(name)
425 defer C.free(unsafe.Pointer(cname))
426 C.LLVMAddNamedMetadataOperand(m.C, cname, operand.C)
438 func (m Module) AddNamedMetadataOperand(name string, operand Metadata) {
439 cname := C.CString(name)
440 defer C.free(unsafe.Pointer(cname))
441 C.LLVMAddNamedMetadataOperand2(m.C, cname, operand.C)
427442 }
428443
429444 func (m Module) Context() (c Context) {
627642 rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind))
628643 return
629644 }
630 func (v Value) SetMetadata(kind int, node Value) {
631 C.LLVMSetMetadata(v.C, C.unsigned(kind), node.C)
645 func (v Value) SetMetadata(kind int, node Metadata) {
646 C.LLVMSetMetadata2(v.C, C.unsigned(kind), node.C)
632647 }
633648
634649 // Conversion functions.
722737 func ConstPointerNull(t Type) (v Value) { v.C = C.LLVMConstPointerNull(t.C); return }
723738
724739 // Operations on metadata
725 func (c Context) MDString(str string) (v Value) {
740 func (c Context) MDString(str string) (md Metadata) {
726741 cstr := C.CString(str)
727742 defer C.free(unsafe.Pointer(cstr))
728 v.C = C.LLVMMDStringInContext(c.C, cstr, C.unsigned(len(str)))
729 return
730 }
731 func (c Context) MDNode(vals []Value) (v Value) {
732 ptr, nvals := llvmValueRefs(vals)
733 v.C = C.LLVMMDNodeInContext(c.C, ptr, nvals)
743 md.C = C.LLVMMDString2(c.C, cstr, C.unsigned(len(str)))
744 return
745 }
746 func (c Context) MDNode(mds []Metadata) (md Metadata) {
747 ptr, nvals := llvmMetadataRefs(mds)
748 md.C = C.LLVMMDNode2(c.C, ptr, nvals)
734749 return
735750 }
736751
11761191 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
11771192
11781193 // Metadata
1179 func (b Builder) SetCurrentDebugLocation(v Value) { C.LLVMSetCurrentDebugLocation(b.C, v.C) }
1180 func (b Builder) CurrentDebugLocation() (v Value) { v.C = C.LLVMGetCurrentDebugLocation(b.C); return }
1194 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
1195 C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
1196 }
11811197 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
11821198 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
11831199 f := module.NamedFunction("llvm.dbg.declare")