1*0a6a1f1dSLionel Sambuc//===- dibuilder.go - Bindings for DIBuilder ------------------------------===//
2*0a6a1f1dSLionel Sambuc//
3*0a6a1f1dSLionel Sambuc//                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc//
5*0a6a1f1dSLionel Sambuc// This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc// License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc//
8*0a6a1f1dSLionel Sambuc//===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc//
10*0a6a1f1dSLionel Sambuc// This file defines bindings for the DIBuilder class.
11*0a6a1f1dSLionel Sambuc//
12*0a6a1f1dSLionel Sambuc//===----------------------------------------------------------------------===//
13*0a6a1f1dSLionel Sambuc
14*0a6a1f1dSLionel Sambucpackage llvm
15*0a6a1f1dSLionel Sambuc
16*0a6a1f1dSLionel Sambuc/*
17*0a6a1f1dSLionel Sambuc#include "DIBuilderBindings.h"
18*0a6a1f1dSLionel Sambuc#include <stdlib.h>
19*0a6a1f1dSLionel Sambuc*/
20*0a6a1f1dSLionel Sambucimport "C"
21*0a6a1f1dSLionel Sambuc
22*0a6a1f1dSLionel Sambucimport (
23*0a6a1f1dSLionel Sambuc	"debug/dwarf"
24*0a6a1f1dSLionel Sambuc	"unsafe"
25*0a6a1f1dSLionel Sambuc)
26*0a6a1f1dSLionel Sambuc
27*0a6a1f1dSLionel Sambuctype DwarfTag uint32
28*0a6a1f1dSLionel Sambuc
29*0a6a1f1dSLionel Sambucconst (
30*0a6a1f1dSLionel Sambuc	DW_TAG_lexical_block   DwarfTag = 0x0b
31*0a6a1f1dSLionel Sambuc	DW_TAG_compile_unit    DwarfTag = 0x11
32*0a6a1f1dSLionel Sambuc	DW_TAG_variable        DwarfTag = 0x34
33*0a6a1f1dSLionel Sambuc	DW_TAG_base_type       DwarfTag = 0x24
34*0a6a1f1dSLionel Sambuc	DW_TAG_pointer_type    DwarfTag = 0x0F
35*0a6a1f1dSLionel Sambuc	DW_TAG_structure_type  DwarfTag = 0x13
36*0a6a1f1dSLionel Sambuc	DW_TAG_subroutine_type DwarfTag = 0x15
37*0a6a1f1dSLionel Sambuc	DW_TAG_file_type       DwarfTag = 0x29
38*0a6a1f1dSLionel Sambuc	DW_TAG_subprogram      DwarfTag = 0x2E
39*0a6a1f1dSLionel Sambuc	DW_TAG_auto_variable   DwarfTag = 0x100
40*0a6a1f1dSLionel Sambuc	DW_TAG_arg_variable    DwarfTag = 0x101
41*0a6a1f1dSLionel Sambuc)
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambucconst (
44*0a6a1f1dSLionel Sambuc	FlagPrivate = 1 << iota
45*0a6a1f1dSLionel Sambuc	FlagProtected
46*0a6a1f1dSLionel Sambuc	FlagFwdDecl
47*0a6a1f1dSLionel Sambuc	FlagAppleBlock
48*0a6a1f1dSLionel Sambuc	FlagBlockByrefStruct
49*0a6a1f1dSLionel Sambuc	FlagVirtual
50*0a6a1f1dSLionel Sambuc	FlagArtificial
51*0a6a1f1dSLionel Sambuc	FlagExplicit
52*0a6a1f1dSLionel Sambuc	FlagPrototyped
53*0a6a1f1dSLionel Sambuc	FlagObjcClassComplete
54*0a6a1f1dSLionel Sambuc	FlagObjectPointer
55*0a6a1f1dSLionel Sambuc	FlagVector
56*0a6a1f1dSLionel Sambuc	FlagStaticMember
57*0a6a1f1dSLionel Sambuc	FlagIndirectVariable
58*0a6a1f1dSLionel Sambuc)
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuctype DwarfLang uint32
61*0a6a1f1dSLionel Sambuc
62*0a6a1f1dSLionel Sambucconst (
63*0a6a1f1dSLionel Sambuc	// http://dwarfstd.org/ShowIssue.php?issue=101014.1&type=open
64*0a6a1f1dSLionel Sambuc	DW_LANG_Go DwarfLang = 0x0016
65*0a6a1f1dSLionel Sambuc)
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuctype DwarfTypeEncoding uint32
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambucconst (
70*0a6a1f1dSLionel Sambuc	DW_ATE_address         DwarfTypeEncoding = 0x01
71*0a6a1f1dSLionel Sambuc	DW_ATE_boolean         DwarfTypeEncoding = 0x02
72*0a6a1f1dSLionel Sambuc	DW_ATE_complex_float   DwarfTypeEncoding = 0x03
73*0a6a1f1dSLionel Sambuc	DW_ATE_float           DwarfTypeEncoding = 0x04
74*0a6a1f1dSLionel Sambuc	DW_ATE_signed          DwarfTypeEncoding = 0x05
75*0a6a1f1dSLionel Sambuc	DW_ATE_signed_char     DwarfTypeEncoding = 0x06
76*0a6a1f1dSLionel Sambuc	DW_ATE_unsigned        DwarfTypeEncoding = 0x07
77*0a6a1f1dSLionel Sambuc	DW_ATE_unsigned_char   DwarfTypeEncoding = 0x08
78*0a6a1f1dSLionel Sambuc	DW_ATE_imaginary_float DwarfTypeEncoding = 0x09
79*0a6a1f1dSLionel Sambuc	DW_ATE_packed_decimal  DwarfTypeEncoding = 0x0a
80*0a6a1f1dSLionel Sambuc	DW_ATE_numeric_string  DwarfTypeEncoding = 0x0b
81*0a6a1f1dSLionel Sambuc	DW_ATE_edited          DwarfTypeEncoding = 0x0c
82*0a6a1f1dSLionel Sambuc	DW_ATE_signed_fixed    DwarfTypeEncoding = 0x0d
83*0a6a1f1dSLionel Sambuc	DW_ATE_unsigned_fixed  DwarfTypeEncoding = 0x0e
84*0a6a1f1dSLionel Sambuc	DW_ATE_decimal_float   DwarfTypeEncoding = 0x0f
85*0a6a1f1dSLionel Sambuc	DW_ATE_UTF             DwarfTypeEncoding = 0x10
86*0a6a1f1dSLionel Sambuc	DW_ATE_lo_user         DwarfTypeEncoding = 0x80
87*0a6a1f1dSLionel Sambuc	DW_ATE_hi_user         DwarfTypeEncoding = 0xff
88*0a6a1f1dSLionel Sambuc)
89*0a6a1f1dSLionel Sambuc
90*0a6a1f1dSLionel Sambuc// DIBuilder is a wrapper for the LLVM DIBuilder class.
91*0a6a1f1dSLionel Sambuctype DIBuilder struct {
92*0a6a1f1dSLionel Sambuc	ref C.LLVMDIBuilderRef
93*0a6a1f1dSLionel Sambuc	m   Module
94*0a6a1f1dSLionel Sambuc}
95*0a6a1f1dSLionel Sambuc
96*0a6a1f1dSLionel Sambuc// NewDIBuilder creates a new DIBuilder, associated with the given module.
97*0a6a1f1dSLionel Sambucfunc NewDIBuilder(m Module) *DIBuilder {
98*0a6a1f1dSLionel Sambuc	d := C.LLVMNewDIBuilder(m.C)
99*0a6a1f1dSLionel Sambuc	return &DIBuilder{ref: d, m: m}
100*0a6a1f1dSLionel Sambuc}
101*0a6a1f1dSLionel Sambuc
102*0a6a1f1dSLionel Sambuc// Destroy destroys the DIBuilder.
103*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) Destroy() {
104*0a6a1f1dSLionel Sambuc	C.LLVMDIBuilderDestroy(d.ref)
105*0a6a1f1dSLionel Sambuc}
106*0a6a1f1dSLionel Sambuc
107*0a6a1f1dSLionel Sambuc// FInalize finalizes the debug information generated by the DIBuilder.
108*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) Finalize() {
109*0a6a1f1dSLionel Sambuc	C.LLVMDIBuilderFinalize(d.ref)
110*0a6a1f1dSLionel Sambuc}
111*0a6a1f1dSLionel Sambuc
112*0a6a1f1dSLionel Sambuc// DICompileUnit holds the values for creating compile unit debug metadata.
113*0a6a1f1dSLionel Sambuctype DICompileUnit struct {
114*0a6a1f1dSLionel Sambuc	Language       DwarfLang
115*0a6a1f1dSLionel Sambuc	File           string
116*0a6a1f1dSLionel Sambuc	Dir            string
117*0a6a1f1dSLionel Sambuc	Producer       string
118*0a6a1f1dSLionel Sambuc	Optimized      bool
119*0a6a1f1dSLionel Sambuc	Flags          string
120*0a6a1f1dSLionel Sambuc	RuntimeVersion int
121*0a6a1f1dSLionel Sambuc}
122*0a6a1f1dSLionel Sambuc
123*0a6a1f1dSLionel Sambuc// CreateCompileUnit creates compile unit debug metadata.
124*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Metadata {
125*0a6a1f1dSLionel Sambuc	file := C.CString(cu.File)
126*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(file))
127*0a6a1f1dSLionel Sambuc	dir := C.CString(cu.Dir)
128*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(dir))
129*0a6a1f1dSLionel Sambuc	producer := C.CString(cu.Producer)
130*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(producer))
131*0a6a1f1dSLionel Sambuc	flags := C.CString(cu.Flags)
132*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(flags))
133*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateCompileUnit(
134*0a6a1f1dSLionel Sambuc		d.ref,
135*0a6a1f1dSLionel Sambuc		C.unsigned(cu.Language),
136*0a6a1f1dSLionel Sambuc		file, dir,
137*0a6a1f1dSLionel Sambuc		producer,
138*0a6a1f1dSLionel Sambuc		boolToCInt(cu.Optimized),
139*0a6a1f1dSLionel Sambuc		flags,
140*0a6a1f1dSLionel Sambuc		C.unsigned(cu.RuntimeVersion),
141*0a6a1f1dSLionel Sambuc	)
142*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
143*0a6a1f1dSLionel Sambuc}
144*0a6a1f1dSLionel Sambuc
145*0a6a1f1dSLionel Sambuc// CreateCompileUnit creates file debug metadata.
146*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateFile(filename, dir string) Metadata {
147*0a6a1f1dSLionel Sambuc	cfilename := C.CString(filename)
148*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(cfilename))
149*0a6a1f1dSLionel Sambuc	cdir := C.CString(dir)
150*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(cdir))
151*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateFile(d.ref, cfilename, cdir)
152*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
153*0a6a1f1dSLionel Sambuc}
154*0a6a1f1dSLionel Sambuc
155*0a6a1f1dSLionel Sambuc// DILexicalBlock holds the values for creating lexical block debug metadata.
156*0a6a1f1dSLionel Sambuctype DILexicalBlock struct {
157*0a6a1f1dSLionel Sambuc	File   Metadata
158*0a6a1f1dSLionel Sambuc	Line   int
159*0a6a1f1dSLionel Sambuc	Column int
160*0a6a1f1dSLionel Sambuc}
161*0a6a1f1dSLionel Sambuc
162*0a6a1f1dSLionel Sambuc// CreateCompileUnit creates lexical block debug metadata.
163*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateLexicalBlock(diScope Metadata, b DILexicalBlock) Metadata {
164*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateLexicalBlock(
165*0a6a1f1dSLionel Sambuc		d.ref,
166*0a6a1f1dSLionel Sambuc		diScope.C,
167*0a6a1f1dSLionel Sambuc		b.File.C,
168*0a6a1f1dSLionel Sambuc		C.unsigned(b.Line),
169*0a6a1f1dSLionel Sambuc		C.unsigned(b.Column),
170*0a6a1f1dSLionel Sambuc	)
171*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
172*0a6a1f1dSLionel Sambuc}
173*0a6a1f1dSLionel Sambuc
174*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateLexicalBlockFile(diScope Metadata, diFile Metadata, discriminator int) Metadata {
175*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateLexicalBlockFile(d.ref, diScope.C, diFile.C,
176*0a6a1f1dSLionel Sambuc		C.unsigned(discriminator))
177*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
178*0a6a1f1dSLionel Sambuc}
179*0a6a1f1dSLionel Sambuc
180*0a6a1f1dSLionel Sambuc// DIFunction holds the values for creating function debug metadata.
181*0a6a1f1dSLionel Sambuctype DIFunction struct {
182*0a6a1f1dSLionel Sambuc	Name         string
183*0a6a1f1dSLionel Sambuc	LinkageName  string
184*0a6a1f1dSLionel Sambuc	File         Metadata
185*0a6a1f1dSLionel Sambuc	Line         int
186*0a6a1f1dSLionel Sambuc	Type         Metadata
187*0a6a1f1dSLionel Sambuc	LocalToUnit  bool
188*0a6a1f1dSLionel Sambuc	IsDefinition bool
189*0a6a1f1dSLionel Sambuc	ScopeLine    int
190*0a6a1f1dSLionel Sambuc	Flags        int
191*0a6a1f1dSLionel Sambuc	Optimized    bool
192*0a6a1f1dSLionel Sambuc	Function     Value
193*0a6a1f1dSLionel Sambuc}
194*0a6a1f1dSLionel Sambuc
195*0a6a1f1dSLionel Sambuc// CreateCompileUnit creates function debug metadata.
196*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateFunction(diScope Metadata, f DIFunction) Metadata {
197*0a6a1f1dSLionel Sambuc	name := C.CString(f.Name)
198*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(name))
199*0a6a1f1dSLionel Sambuc	linkageName := C.CString(f.LinkageName)
200*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(linkageName))
201*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateFunction(
202*0a6a1f1dSLionel Sambuc		d.ref,
203*0a6a1f1dSLionel Sambuc		diScope.C,
204*0a6a1f1dSLionel Sambuc		name,
205*0a6a1f1dSLionel Sambuc		linkageName,
206*0a6a1f1dSLionel Sambuc		f.File.C,
207*0a6a1f1dSLionel Sambuc		C.unsigned(f.Line),
208*0a6a1f1dSLionel Sambuc		f.Type.C,
209*0a6a1f1dSLionel Sambuc		boolToCInt(f.LocalToUnit),
210*0a6a1f1dSLionel Sambuc		boolToCInt(f.IsDefinition),
211*0a6a1f1dSLionel Sambuc		C.unsigned(f.ScopeLine),
212*0a6a1f1dSLionel Sambuc		C.unsigned(f.Flags),
213*0a6a1f1dSLionel Sambuc		boolToCInt(f.Optimized),
214*0a6a1f1dSLionel Sambuc		f.Function.C,
215*0a6a1f1dSLionel Sambuc	)
216*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
217*0a6a1f1dSLionel Sambuc}
218*0a6a1f1dSLionel Sambuc
219*0a6a1f1dSLionel Sambuc// DILocalVariable holds the values for creating local variable debug metadata.
220*0a6a1f1dSLionel Sambuctype DILocalVariable struct {
221*0a6a1f1dSLionel Sambuc	Tag            dwarf.Tag
222*0a6a1f1dSLionel Sambuc	Name           string
223*0a6a1f1dSLionel Sambuc	File           Metadata
224*0a6a1f1dSLionel Sambuc	Line           int
225*0a6a1f1dSLionel Sambuc	Type           Metadata
226*0a6a1f1dSLionel Sambuc	AlwaysPreserve bool
227*0a6a1f1dSLionel Sambuc	Flags          int
228*0a6a1f1dSLionel Sambuc
229*0a6a1f1dSLionel Sambuc	// ArgNo is the 1-based index of the argument in the function's
230*0a6a1f1dSLionel Sambuc	// parameter list if it is an argument, or 0 otherwise.
231*0a6a1f1dSLionel Sambuc	ArgNo int
232*0a6a1f1dSLionel Sambuc}
233*0a6a1f1dSLionel Sambuc
234*0a6a1f1dSLionel Sambuc// CreateLocalVariable creates local variable debug metadata.
235*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateLocalVariable(scope Metadata, v DILocalVariable) Metadata {
236*0a6a1f1dSLionel Sambuc	name := C.CString(v.Name)
237*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(name))
238*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateLocalVariable(
239*0a6a1f1dSLionel Sambuc		d.ref,
240*0a6a1f1dSLionel Sambuc		C.unsigned(v.Tag),
241*0a6a1f1dSLionel Sambuc		scope.C,
242*0a6a1f1dSLionel Sambuc		name,
243*0a6a1f1dSLionel Sambuc		v.File.C,
244*0a6a1f1dSLionel Sambuc		C.unsigned(v.Line),
245*0a6a1f1dSLionel Sambuc		v.Type.C,
246*0a6a1f1dSLionel Sambuc		boolToCInt(v.AlwaysPreserve),
247*0a6a1f1dSLionel Sambuc		C.unsigned(v.Flags),
248*0a6a1f1dSLionel Sambuc		C.unsigned(v.ArgNo),
249*0a6a1f1dSLionel Sambuc	)
250*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
251*0a6a1f1dSLionel Sambuc}
252*0a6a1f1dSLionel Sambuc
253*0a6a1f1dSLionel Sambuc// DIBasicType holds the values for creating basic type debug metadata.
254*0a6a1f1dSLionel Sambuctype DIBasicType struct {
255*0a6a1f1dSLionel Sambuc	Name        string
256*0a6a1f1dSLionel Sambuc	SizeInBits  uint64
257*0a6a1f1dSLionel Sambuc	AlignInBits uint64
258*0a6a1f1dSLionel Sambuc	Encoding    DwarfTypeEncoding
259*0a6a1f1dSLionel Sambuc}
260*0a6a1f1dSLionel Sambuc
261*0a6a1f1dSLionel Sambuc// CreateBasicType creates basic type debug metadata.
262*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateBasicType(t DIBasicType) Metadata {
263*0a6a1f1dSLionel Sambuc	name := C.CString(t.Name)
264*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(name))
265*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateBasicType(
266*0a6a1f1dSLionel Sambuc		d.ref,
267*0a6a1f1dSLionel Sambuc		name,
268*0a6a1f1dSLionel Sambuc		C.uint64_t(t.SizeInBits),
269*0a6a1f1dSLionel Sambuc		C.uint64_t(t.AlignInBits),
270*0a6a1f1dSLionel Sambuc		C.unsigned(t.Encoding),
271*0a6a1f1dSLionel Sambuc	)
272*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
273*0a6a1f1dSLionel Sambuc}
274*0a6a1f1dSLionel Sambuc
275*0a6a1f1dSLionel Sambuc// DIPointerType holds the values for creating pointer type debug metadata.
276*0a6a1f1dSLionel Sambuctype DIPointerType struct {
277*0a6a1f1dSLionel Sambuc	Pointee     Metadata
278*0a6a1f1dSLionel Sambuc	SizeInBits  uint64
279*0a6a1f1dSLionel Sambuc	AlignInBits uint64 // optional
280*0a6a1f1dSLionel Sambuc	Name        string // optional
281*0a6a1f1dSLionel Sambuc}
282*0a6a1f1dSLionel Sambuc
283*0a6a1f1dSLionel Sambuc// CreateBasicType creates basic type debug metadata.
284*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreatePointerType(t DIPointerType) Metadata {
285*0a6a1f1dSLionel Sambuc	name := C.CString(t.Name)
286*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(name))
287*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreatePointerType(
288*0a6a1f1dSLionel Sambuc		d.ref,
289*0a6a1f1dSLionel Sambuc		t.Pointee.C,
290*0a6a1f1dSLionel Sambuc		C.uint64_t(t.SizeInBits),
291*0a6a1f1dSLionel Sambuc		C.uint64_t(t.AlignInBits),
292*0a6a1f1dSLionel Sambuc		name,
293*0a6a1f1dSLionel Sambuc	)
294*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
295*0a6a1f1dSLionel Sambuc}
296*0a6a1f1dSLionel Sambuc
297*0a6a1f1dSLionel Sambuc// DISubroutineType holds the values for creating subroutine type debug metadata.
298*0a6a1f1dSLionel Sambuctype DISubroutineType struct {
299*0a6a1f1dSLionel Sambuc	// File is the file in which the subroutine type is defined.
300*0a6a1f1dSLionel Sambuc	File Metadata
301*0a6a1f1dSLionel Sambuc
302*0a6a1f1dSLionel Sambuc	// Parameters contains the subroutine parameter types,
303*0a6a1f1dSLionel Sambuc	// including the return type at the 0th index.
304*0a6a1f1dSLionel Sambuc	Parameters []Metadata
305*0a6a1f1dSLionel Sambuc}
306*0a6a1f1dSLionel Sambuc
307*0a6a1f1dSLionel Sambuc// CreateSubroutineType creates subroutine type debug metadata.
308*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateSubroutineType(t DISubroutineType) Metadata {
309*0a6a1f1dSLionel Sambuc	params := d.getOrCreateTypeArray(t.Parameters)
310*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateSubroutineType(d.ref, t.File.C, params.C)
311*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
312*0a6a1f1dSLionel Sambuc}
313*0a6a1f1dSLionel Sambuc
314*0a6a1f1dSLionel Sambuc// DIStructType holds the values for creating struct type debug metadata.
315*0a6a1f1dSLionel Sambuctype DIStructType struct {
316*0a6a1f1dSLionel Sambuc	Name        string
317*0a6a1f1dSLionel Sambuc	File        Metadata
318*0a6a1f1dSLionel Sambuc	Line        int
319*0a6a1f1dSLionel Sambuc	SizeInBits  uint64
320*0a6a1f1dSLionel Sambuc	AlignInBits uint64
321*0a6a1f1dSLionel Sambuc	Flags       int
322*0a6a1f1dSLionel Sambuc	DerivedFrom Metadata
323*0a6a1f1dSLionel Sambuc	Elements    []Metadata
324*0a6a1f1dSLionel Sambuc}
325*0a6a1f1dSLionel Sambuc
326*0a6a1f1dSLionel Sambuc// CreateStructType creates struct type debug metadata.
327*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateStructType(scope Metadata, t DIStructType) Metadata {
328*0a6a1f1dSLionel Sambuc	elements := d.getOrCreateArray(t.Elements)
329*0a6a1f1dSLionel Sambuc	name := C.CString(t.Name)
330*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(name))
331*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateStructType(
332*0a6a1f1dSLionel Sambuc		d.ref,
333*0a6a1f1dSLionel Sambuc		scope.C,
334*0a6a1f1dSLionel Sambuc		name,
335*0a6a1f1dSLionel Sambuc		t.File.C,
336*0a6a1f1dSLionel Sambuc		C.unsigned(t.Line),
337*0a6a1f1dSLionel Sambuc		C.uint64_t(t.SizeInBits),
338*0a6a1f1dSLionel Sambuc		C.uint64_t(t.AlignInBits),
339*0a6a1f1dSLionel Sambuc		C.unsigned(t.Flags),
340*0a6a1f1dSLionel Sambuc		t.DerivedFrom.C,
341*0a6a1f1dSLionel Sambuc		elements.C,
342*0a6a1f1dSLionel Sambuc	)
343*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
344*0a6a1f1dSLionel Sambuc}
345*0a6a1f1dSLionel Sambuc
346*0a6a1f1dSLionel Sambuc// DIMemberType holds the values for creating member type debug metadata.
347*0a6a1f1dSLionel Sambuctype DIMemberType struct {
348*0a6a1f1dSLionel Sambuc	Name         string
349*0a6a1f1dSLionel Sambuc	File         Metadata
350*0a6a1f1dSLionel Sambuc	Line         int
351*0a6a1f1dSLionel Sambuc	SizeInBits   uint64
352*0a6a1f1dSLionel Sambuc	AlignInBits  uint64
353*0a6a1f1dSLionel Sambuc	OffsetInBits uint64
354*0a6a1f1dSLionel Sambuc	Flags        int
355*0a6a1f1dSLionel Sambuc	Type         Metadata
356*0a6a1f1dSLionel Sambuc}
357*0a6a1f1dSLionel Sambuc
358*0a6a1f1dSLionel Sambuc// CreateMemberType creates struct type debug metadata.
359*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateMemberType(scope Metadata, t DIMemberType) Metadata {
360*0a6a1f1dSLionel Sambuc	name := C.CString(t.Name)
361*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(name))
362*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateMemberType(
363*0a6a1f1dSLionel Sambuc		d.ref,
364*0a6a1f1dSLionel Sambuc		scope.C,
365*0a6a1f1dSLionel Sambuc		name,
366*0a6a1f1dSLionel Sambuc		t.File.C,
367*0a6a1f1dSLionel Sambuc		C.unsigned(t.Line),
368*0a6a1f1dSLionel Sambuc		C.uint64_t(t.SizeInBits),
369*0a6a1f1dSLionel Sambuc		C.uint64_t(t.AlignInBits),
370*0a6a1f1dSLionel Sambuc		C.uint64_t(t.OffsetInBits),
371*0a6a1f1dSLionel Sambuc		C.unsigned(t.Flags),
372*0a6a1f1dSLionel Sambuc		t.Type.C,
373*0a6a1f1dSLionel Sambuc	)
374*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
375*0a6a1f1dSLionel Sambuc}
376*0a6a1f1dSLionel Sambuc
377*0a6a1f1dSLionel Sambuc// DISubrange describes an integer value range.
378*0a6a1f1dSLionel Sambuctype DISubrange struct {
379*0a6a1f1dSLionel Sambuc	Lo    int64
380*0a6a1f1dSLionel Sambuc	Count int64
381*0a6a1f1dSLionel Sambuc}
382*0a6a1f1dSLionel Sambuc
383*0a6a1f1dSLionel Sambuc// DIArrayType holds the values for creating array type debug metadata.
384*0a6a1f1dSLionel Sambuctype DIArrayType struct {
385*0a6a1f1dSLionel Sambuc	SizeInBits  uint64
386*0a6a1f1dSLionel Sambuc	AlignInBits uint64
387*0a6a1f1dSLionel Sambuc	ElementType Metadata
388*0a6a1f1dSLionel Sambuc	Subscripts  []DISubrange
389*0a6a1f1dSLionel Sambuc}
390*0a6a1f1dSLionel Sambuc
391*0a6a1f1dSLionel Sambuc// CreateArrayType creates struct type debug metadata.
392*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateArrayType(t DIArrayType) Metadata {
393*0a6a1f1dSLionel Sambuc	subscriptsSlice := make([]Metadata, len(t.Subscripts))
394*0a6a1f1dSLionel Sambuc	for i, s := range t.Subscripts {
395*0a6a1f1dSLionel Sambuc		subscriptsSlice[i] = d.getOrCreateSubrange(s.Lo, s.Count)
396*0a6a1f1dSLionel Sambuc	}
397*0a6a1f1dSLionel Sambuc	subscripts := d.getOrCreateArray(subscriptsSlice)
398*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateArrayType(
399*0a6a1f1dSLionel Sambuc		d.ref,
400*0a6a1f1dSLionel Sambuc		C.uint64_t(t.SizeInBits),
401*0a6a1f1dSLionel Sambuc		C.uint64_t(t.AlignInBits),
402*0a6a1f1dSLionel Sambuc		t.ElementType.C,
403*0a6a1f1dSLionel Sambuc		subscripts.C,
404*0a6a1f1dSLionel Sambuc	)
405*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
406*0a6a1f1dSLionel Sambuc}
407*0a6a1f1dSLionel Sambuc
408*0a6a1f1dSLionel Sambuc// DITypedef holds the values for creating typedef type debug metadata.
409*0a6a1f1dSLionel Sambuctype DITypedef struct {
410*0a6a1f1dSLionel Sambuc	Type    Metadata
411*0a6a1f1dSLionel Sambuc	Name    string
412*0a6a1f1dSLionel Sambuc	File    Metadata
413*0a6a1f1dSLionel Sambuc	Line    int
414*0a6a1f1dSLionel Sambuc	Context Metadata
415*0a6a1f1dSLionel Sambuc}
416*0a6a1f1dSLionel Sambuc
417*0a6a1f1dSLionel Sambuc// CreateTypedef creates typedef type debug metadata.
418*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateTypedef(t DITypedef) Metadata {
419*0a6a1f1dSLionel Sambuc	name := C.CString(t.Name)
420*0a6a1f1dSLionel Sambuc	defer C.free(unsafe.Pointer(name))
421*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateTypedef(
422*0a6a1f1dSLionel Sambuc		d.ref,
423*0a6a1f1dSLionel Sambuc		t.Type.C,
424*0a6a1f1dSLionel Sambuc		name,
425*0a6a1f1dSLionel Sambuc		t.File.C,
426*0a6a1f1dSLionel Sambuc		C.unsigned(t.Line),
427*0a6a1f1dSLionel Sambuc		t.Context.C,
428*0a6a1f1dSLionel Sambuc	)
429*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
430*0a6a1f1dSLionel Sambuc}
431*0a6a1f1dSLionel Sambuc
432*0a6a1f1dSLionel Sambuc// getOrCreateSubrange gets a metadata node for the specified subrange,
433*0a6a1f1dSLionel Sambuc// creating if required.
434*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) getOrCreateSubrange(lo, count int64) Metadata {
435*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderGetOrCreateSubrange(d.ref, C.int64_t(lo), C.int64_t(count))
436*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
437*0a6a1f1dSLionel Sambuc}
438*0a6a1f1dSLionel Sambuc
439*0a6a1f1dSLionel Sambuc// getOrCreateArray gets a metadata node containing the specified values,
440*0a6a1f1dSLionel Sambuc// creating if required.
441*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) getOrCreateArray(values []Metadata) Metadata {
442*0a6a1f1dSLionel Sambuc	if len(values) == 0 {
443*0a6a1f1dSLionel Sambuc		return Metadata{}
444*0a6a1f1dSLionel Sambuc	}
445*0a6a1f1dSLionel Sambuc	data, length := llvmMetadataRefs(values)
446*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderGetOrCreateArray(d.ref, data, C.size_t(length))
447*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
448*0a6a1f1dSLionel Sambuc}
449*0a6a1f1dSLionel Sambuc
450*0a6a1f1dSLionel Sambuc// getOrCreateTypeArray gets a metadata node for a type array containing the
451*0a6a1f1dSLionel Sambuc// specified values, creating if required.
452*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) getOrCreateTypeArray(values []Metadata) Metadata {
453*0a6a1f1dSLionel Sambuc	if len(values) == 0 {
454*0a6a1f1dSLionel Sambuc		return Metadata{}
455*0a6a1f1dSLionel Sambuc	}
456*0a6a1f1dSLionel Sambuc	data, length := llvmMetadataRefs(values)
457*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderGetOrCreateTypeArray(d.ref, data, C.size_t(length))
458*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
459*0a6a1f1dSLionel Sambuc}
460*0a6a1f1dSLionel Sambuc
461*0a6a1f1dSLionel Sambuc// CreateExpression creates a new descriptor for the specified
462*0a6a1f1dSLionel Sambuc// variable which has a complex address expression for its address.
463*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) CreateExpression(addr []int64) Metadata {
464*0a6a1f1dSLionel Sambuc	var data *C.int64_t
465*0a6a1f1dSLionel Sambuc	if len(addr) > 0 {
466*0a6a1f1dSLionel Sambuc		data = (*C.int64_t)(unsafe.Pointer(&addr[0]))
467*0a6a1f1dSLionel Sambuc	}
468*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderCreateExpression(d.ref, data, C.size_t(len(addr)))
469*0a6a1f1dSLionel Sambuc	return Metadata{C: result}
470*0a6a1f1dSLionel Sambuc}
471*0a6a1f1dSLionel Sambuc
472*0a6a1f1dSLionel Sambuc// InsertDeclareAtEnd inserts a call to llvm.dbg.declare at the end of the
473*0a6a1f1dSLionel Sambuc// specified basic block for the given value and associated debug metadata.
474*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) InsertDeclareAtEnd(v Value, diVarInfo, expr Metadata, bb BasicBlock) Value {
475*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderInsertDeclareAtEnd(d.ref, v.C, diVarInfo.C, expr.C, bb.C)
476*0a6a1f1dSLionel Sambuc	return Value{C: result}
477*0a6a1f1dSLionel Sambuc}
478*0a6a1f1dSLionel Sambuc
479*0a6a1f1dSLionel Sambuc// InsertValueAtEnd inserts a call to llvm.dbg.value at the end of the
480*0a6a1f1dSLionel Sambuc// specified basic block for the given value and associated debug metadata.
481*0a6a1f1dSLionel Sambucfunc (d *DIBuilder) InsertValueAtEnd(v Value, diVarInfo, expr Metadata, offset uint64, bb BasicBlock) Value {
482*0a6a1f1dSLionel Sambuc	result := C.LLVMDIBuilderInsertValueAtEnd(d.ref, v.C, C.uint64_t(offset), diVarInfo.C, expr.C, bb.C)
483*0a6a1f1dSLionel Sambuc	return Value{C: result}
484*0a6a1f1dSLionel Sambuc}
485*0a6a1f1dSLionel Sambuc
486*0a6a1f1dSLionel Sambucfunc boolToCInt(v bool) C.int {
487*0a6a1f1dSLionel Sambuc	if v {
488*0a6a1f1dSLionel Sambuc		return 1
489*0a6a1f1dSLionel Sambuc	}
490*0a6a1f1dSLionel Sambuc	return 0
491*0a6a1f1dSLionel Sambuc}
492