reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487
  488
  489
  490
  491
  492
  493
  494
  495
  496
  497
  498
  499
  500
  501
  502
  503
  504
  505
  506
  507
  508
  509
  510
  511
  512
  513
  514
  515
  516
  517
  518
  519
  520
  521
  522
  523
  524
  525
  526
  527
  528
  529
  530
  531
  532
  533
  534
  535
  536
  537
  538
  539
  540
  541
  542
  543
  544
  545
  546
  547
  548
  549
  550
  551
  552
  553
  554
  555
  556
  557
  558
  559
  560
  561
  562
  563
  564
  565
  566
  567
  568
  569
  570
  571
  572
  573
  574
  575
  576
  577
  578
  579
  580
  581
  582
  583
  584
  585
  586
  587
  588
  589
  590
  591
  592
  593
  594
  595
  596
  597
  598
  599
  600
  601
  602
  603
  604
  605
  606
  607
  608
  609
  610
  611
  612
  613
  614
  615
  616
  617
  618
  619
  620
  621
  622
  623
  624
  625
  626
  627
  628
  629
  630
  631
  632
  633
  634
  635
  636
  637
  638
  639
  640
  641
  642
  643
  644
  645
  646
  647
  648
  649
  650
  651
  652
  653
  654
  655
  656
  657
  658
  659
  660
  661
  662
  663
  664
  665
  666
  667
  668
  669
  670
  671
  672
  673
  674
  675
  676
  677
  678
  679
  680
  681
  682
  683
  684
  685
  686
  687
  688
  689
  690
  691
  692
  693
  694
  695
  696
  697
//===- dibuilder.go - Bindings for DIBuilder ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines bindings for the DIBuilder class.
//
//===----------------------------------------------------------------------===//

package llvm

/*
#include "IRBindings.h"
#include <stdlib.h>
*/
import "C"

import (
	"debug/dwarf"
	"unsafe"
)

type DwarfTag uint32

const (
	DW_TAG_lexical_block   DwarfTag = 0x0b
	DW_TAG_compile_unit    DwarfTag = 0x11
	DW_TAG_variable        DwarfTag = 0x34
	DW_TAG_base_type       DwarfTag = 0x24
	DW_TAG_pointer_type    DwarfTag = 0x0F
	DW_TAG_structure_type  DwarfTag = 0x13
	DW_TAG_subroutine_type DwarfTag = 0x15
	DW_TAG_file_type       DwarfTag = 0x29
	DW_TAG_subprogram      DwarfTag = 0x2E
	DW_TAG_auto_variable   DwarfTag = 0x100
	DW_TAG_arg_variable    DwarfTag = 0x101
)

const (
	FlagPrivate = 1 << iota
	FlagProtected
	FlagFwdDecl
	FlagAppleBlock
	FlagReserved
	FlagVirtual
	FlagArtificial
	FlagExplicit
	FlagPrototyped
	FlagObjcClassComplete
	FlagObjectPointer
	FlagVector
	FlagStaticMember
	FlagIndirectVariable
	FlagArgumentNotModified
)

type DwarfLang uint32

const (
	// http://dwarfstd.org/ShowIssue.php?issue=101014.1&type=open
	DW_LANG_Go DwarfLang = 0x0016
)

type DwarfTypeEncoding uint32

const (
	DW_ATE_address         DwarfTypeEncoding = 0x01
	DW_ATE_boolean         DwarfTypeEncoding = 0x02
	DW_ATE_complex_float   DwarfTypeEncoding = 0x03
	DW_ATE_float           DwarfTypeEncoding = 0x04
	DW_ATE_signed          DwarfTypeEncoding = 0x05
	DW_ATE_signed_char     DwarfTypeEncoding = 0x06
	DW_ATE_unsigned        DwarfTypeEncoding = 0x07
	DW_ATE_unsigned_char   DwarfTypeEncoding = 0x08
	DW_ATE_imaginary_float DwarfTypeEncoding = 0x09
	DW_ATE_packed_decimal  DwarfTypeEncoding = 0x0a
	DW_ATE_numeric_string  DwarfTypeEncoding = 0x0b
	DW_ATE_edited          DwarfTypeEncoding = 0x0c
	DW_ATE_signed_fixed    DwarfTypeEncoding = 0x0d
	DW_ATE_unsigned_fixed  DwarfTypeEncoding = 0x0e
	DW_ATE_decimal_float   DwarfTypeEncoding = 0x0f
	DW_ATE_UTF             DwarfTypeEncoding = 0x10
	DW_ATE_lo_user         DwarfTypeEncoding = 0x80
	DW_ATE_hi_user         DwarfTypeEncoding = 0xff
)

// DIBuilder is a wrapper for the LLVM DIBuilder class.
type DIBuilder struct {
	ref C.LLVMDIBuilderRef
	m   Module
}

// NewDIBuilder creates a new DIBuilder, associated with the given module.
func NewDIBuilder(m Module) *DIBuilder {
	d := C.LLVMCreateDIBuilder(m.C)
	return &DIBuilder{ref: d, m: m}
}

// Destroy destroys the DIBuilder.
func (d *DIBuilder) Destroy() {
	C.LLVMDisposeDIBuilder(d.ref)
}

// FInalize finalizes the debug information generated by the DIBuilder.
func (d *DIBuilder) Finalize() {
	C.LLVMDIBuilderFinalize(d.ref)
}

// DICompileUnit holds the values for creating compile unit debug metadata.
type DICompileUnit struct {
	Language       DwarfLang
	File           string
	Dir            string
	Producer       string
	Optimized      bool
	Flags          string
	RuntimeVersion int
}

// CreateCompileUnit creates compile unit debug metadata.
func (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Metadata {
	file := C.CString(cu.File)
	defer C.free(unsafe.Pointer(file))
	dir := C.CString(cu.Dir)
	defer C.free(unsafe.Pointer(dir))
	producer := C.CString(cu.Producer)
	defer C.free(unsafe.Pointer(producer))
	flags := C.CString(cu.Flags)
	defer C.free(unsafe.Pointer(flags))
	result := C.LLVMDIBuilderCreateCompileUnit(
		d.ref,
		C.LLVMDWARFSourceLanguage(cu.Language),
		C.LLVMDIBuilderCreateFile(d.ref, file, C.size_t(len(cu.File)), dir, C.size_t(len(cu.Dir))),
		producer, C.size_t(len(cu.Producer)),
		C.LLVMBool(boolToCInt(cu.Optimized)),
		flags, C.size_t(len(cu.Flags)),
		C.unsigned(cu.RuntimeVersion),
		/*SplitName=*/ nil, 0,
		C.LLVMDWARFEmissionFull,
		/*DWOId=*/ 0,
		/*SplitDebugInlining*/ C.LLVMBool(boolToCInt(true)),
		/*DebugInfoForProfiling*/ C.LLVMBool(boolToCInt(false)),
	)
	return Metadata{C: result}
}

// CreateFile creates file debug metadata.
func (d *DIBuilder) CreateFile(filename, dir string) Metadata {
	cfilename := C.CString(filename)
	defer C.free(unsafe.Pointer(cfilename))
	cdir := C.CString(dir)
	defer C.free(unsafe.Pointer(cdir))
	result := C.LLVMDIBuilderCreateFile(d.ref,
		cfilename, C.size_t(len(filename)),
		cdir, C.size_t(len(dir)))
	return Metadata{C: result}
}

// DILexicalBlock holds the values for creating lexical block debug metadata.
type DILexicalBlock struct {
	File   Metadata
	Line   int
	Column int
}

// CreateLexicalBlock creates lexical block debug metadata.
func (d *DIBuilder) CreateLexicalBlock(diScope Metadata, b DILexicalBlock) Metadata {
	result := C.LLVMDIBuilderCreateLexicalBlock(
		d.ref,
		diScope.C,
		b.File.C,
		C.unsigned(b.Line),
		C.unsigned(b.Column),
	)
	return Metadata{C: result}
}

func (d *DIBuilder) CreateLexicalBlockFile(diScope Metadata, diFile Metadata, discriminator int) Metadata {
	result := C.LLVMDIBuilderCreateLexicalBlockFile(d.ref, diScope.C, diFile.C,
		C.unsigned(discriminator))
	return Metadata{C: result}
}

// DIFunction holds the values for creating function debug metadata.
type DIFunction struct {
	Name         string
	LinkageName  string
	File         Metadata
	Line         int
	Type         Metadata
	LocalToUnit  bool
	IsDefinition bool
	ScopeLine    int
	Flags        int
	Optimized    bool
}

// CreateFunction creates function debug metadata.
func (d *DIBuilder) CreateFunction(diScope Metadata, f DIFunction) Metadata {
	name := C.CString(f.Name)
	defer C.free(unsafe.Pointer(name))
	linkageName := C.CString(f.LinkageName)
	defer C.free(unsafe.Pointer(linkageName))
	result := C.LLVMDIBuilderCreateFunction(
		d.ref,
		diScope.C,
		name, C.size_t(len(f.Name)),
		linkageName, C.size_t(len(f.LinkageName)),
		f.File.C,
		C.unsigned(f.Line),
		f.Type.C,
		C.LLVMBool(boolToCInt(f.LocalToUnit)),
		C.LLVMBool(boolToCInt(f.IsDefinition)),
		C.unsigned(f.ScopeLine),
		C.LLVMDIFlags(f.Flags),
		C.LLVMBool(boolToCInt(f.Optimized)),
	)
	return Metadata{C: result}
}

// DIAutoVariable holds the values for creating auto variable debug metadata.
type DIAutoVariable struct {
	Name           string
	File           Metadata
	Line           int
	Type           Metadata
	AlwaysPreserve bool
	Flags          int
	AlignInBits    uint32
}

// CreateAutoVariable creates local variable debug metadata.
func (d *DIBuilder) CreateAutoVariable(scope Metadata, v DIAutoVariable) Metadata {
	name := C.CString(v.Name)
	defer C.free(unsafe.Pointer(name))
	result := C.LLVMDIBuilderCreateAutoVariable(
		d.ref,
		scope.C,
		name, C.size_t(len(v.Name)),
		v.File.C,
		C.unsigned(v.Line),
		v.Type.C,
		C.LLVMBool(boolToCInt(v.AlwaysPreserve)),
		C.LLVMDIFlags(v.Flags),
		C.uint32_t(v.AlignInBits),
	)
	return Metadata{C: result}
}

// DIParameterVariable holds the values for creating parameter variable debug metadata.
type DIParameterVariable struct {
	Name           string
	File           Metadata
	Line           int
	Type           Metadata
	AlwaysPreserve bool
	Flags          int

	// ArgNo is the 1-based index of the argument in the function's
	// parameter list.
	ArgNo int
}

// CreateParameterVariable creates parameter variable debug metadata.
func (d *DIBuilder) CreateParameterVariable(scope Metadata, v DIParameterVariable) Metadata {
	name := C.CString(v.Name)
	defer C.free(unsafe.Pointer(name))
	result := C.LLVMDIBuilderCreateParameterVariable(
		d.ref,
		scope.C,
		name, C.size_t(len(v.Name)),
		C.unsigned(v.ArgNo),
		v.File.C,
		C.unsigned(v.Line),
		v.Type.C,
		C.LLVMBool(boolToCInt(v.AlwaysPreserve)),
		C.LLVMDIFlags(v.Flags),
	)
	return Metadata{C: result}
}

// DIBasicType holds the values for creating basic type debug metadata.
type DIBasicType struct {
	Name       string
	SizeInBits uint64
	Encoding   DwarfTypeEncoding
}

// CreateBasicType creates basic type debug metadata.
func (d *DIBuilder) CreateBasicType(t DIBasicType) Metadata {
	name := C.CString(t.Name)
	defer C.free(unsafe.Pointer(name))
	result := C.LLVMDIBuilderCreateBasicType(
		d.ref,
		name,
		C.size_t(len(t.Name)),
		C.uint64_t(t.SizeInBits),
		C.LLVMDWARFTypeEncoding(t.Encoding),
		C.LLVMDIFlags(0),
	)
	return Metadata{C: result}
}

// DIPointerType holds the values for creating pointer type debug metadata.
type DIPointerType struct {
	Pointee      Metadata
	SizeInBits   uint64
	AlignInBits  uint32 // optional
	AddressSpace uint32
	Name         string // optional
}

// CreatePointerType creates a type that represents a pointer to another type.
func (d *DIBuilder) CreatePointerType(t DIPointerType) Metadata {
	name := C.CString(t.Name)
	defer C.free(unsafe.Pointer(name))
	result := C.LLVMDIBuilderCreatePointerType(
		d.ref,
		t.Pointee.C,
		C.uint64_t(t.SizeInBits),
		C.uint32_t(t.AlignInBits),
		C.unsigned(t.AddressSpace),
		name,
		C.size_t(len(t.Name)),
	)
	return Metadata{C: result}
}

// DISubroutineType holds the values for creating subroutine type debug metadata.
type DISubroutineType struct {
	// File is the file in which the subroutine type is defined.
	File Metadata

	// Parameters contains the subroutine parameter types,
	// including the return type at the 0th index.
	Parameters []Metadata

	Flags int
}

// CreateSubroutineType creates subroutine type debug metadata.
func (d *DIBuilder) CreateSubroutineType(t DISubroutineType) Metadata {
	params, length := llvmMetadataRefs(t.Parameters)
	result := C.LLVMDIBuilderCreateSubroutineType(
		d.ref,
		t.File.C,
		params,
		length,
		C.LLVMDIFlags(t.Flags),
	)
	return Metadata{C: result}
}

// DIStructType holds the values for creating struct type debug metadata.
type DIStructType struct {
	Name         string
	File         Metadata
	Line         int
	SizeInBits   uint64
	AlignInBits  uint32
	Flags        int
	DerivedFrom  Metadata
	Elements     []Metadata
	VTableHolder Metadata // optional
	UniqueID     string
}

// CreateStructType creates struct type debug metadata.
func (d *DIBuilder) CreateStructType(scope Metadata, t DIStructType) Metadata {
	elements, length := llvmMetadataRefs(t.Elements)
	name := C.CString(t.Name)
	uniqueID := C.CString(t.UniqueID)
	defer C.free(unsafe.Pointer(name))
	defer C.free(unsafe.Pointer(uniqueID))
	result := C.LLVMDIBuilderCreateStructType(
		d.ref,
		scope.C,
		name,
		C.size_t(len(t.Name)),
		t.File.C,
		C.unsigned(t.Line),
		C.uint64_t(t.SizeInBits),
		C.uint32_t(t.AlignInBits),
		C.LLVMDIFlags(t.Flags),
		t.DerivedFrom.C,
		elements,
		length,
		C.unsigned(0), // Optional Objective-C runtime version.
		t.VTableHolder.C,
		uniqueID,
		C.size_t(len(t.UniqueID)),
	)
	return Metadata{C: result}
}

// DIReplaceableCompositeType holds the values for creating replaceable
// composite type debug metadata.
type DIReplaceableCompositeType struct {
	Tag         dwarf.Tag
	Name        string
	File        Metadata
	Line        int
	RuntimeLang int
	SizeInBits  uint64
	AlignInBits uint32
	Flags       int
	UniqueID    string
}

// CreateReplaceableCompositeType creates replaceable composite type debug metadata.
func (d *DIBuilder) CreateReplaceableCompositeType(scope Metadata, t DIReplaceableCompositeType) Metadata {
	name := C.CString(t.Name)
	uniqueID := C.CString(t.UniqueID)
	defer C.free(unsafe.Pointer(name))
	defer C.free(unsafe.Pointer(uniqueID))
	result := C.LLVMDIBuilderCreateReplaceableCompositeType(
		d.ref,
		C.unsigned(t.Tag),
		name,
		C.size_t(len(t.Name)),
		scope.C,
		t.File.C,
		C.unsigned(t.Line),
		C.unsigned(t.RuntimeLang),
		C.uint64_t(t.SizeInBits),
		C.uint32_t(t.AlignInBits),
		C.LLVMDIFlags(t.Flags),
		uniqueID,
		C.size_t(len(t.UniqueID)),
	)
	return Metadata{C: result}
}

// DIMemberType holds the values for creating member type debug metadata.
type DIMemberType struct {
	Name         string
	File         Metadata
	Line         int
	SizeInBits   uint64
	AlignInBits  uint32
	OffsetInBits uint64
	Flags        int
	Type         Metadata
}

// CreateMemberType creates struct type debug metadata.
func (d *DIBuilder) CreateMemberType(scope Metadata, t DIMemberType) Metadata {
	name := C.CString(t.Name)
	defer C.free(unsafe.Pointer(name))
	result := C.LLVMDIBuilderCreateMemberType(
		d.ref,
		scope.C,
		name,
		C.size_t(len(t.Name)),
		t.File.C,
		C.unsigned(t.Line),
		C.uint64_t(t.SizeInBits),
		C.uint32_t(t.AlignInBits),
		C.uint64_t(t.OffsetInBits),
		C.LLVMDIFlags(t.Flags),
		t.Type.C,
	)
	return Metadata{C: result}
}

// DISubrange describes an integer value range.
type DISubrange struct {
	Lo    int64
	Count int64
}

// DIArrayType holds the values for creating array type debug metadata.
type DIArrayType struct {
	SizeInBits  uint64
	AlignInBits uint32
	ElementType Metadata
	Subscripts  []DISubrange
}

// CreateArrayType creates struct type debug metadata.
func (d *DIBuilder) CreateArrayType(t DIArrayType) Metadata {
	subscriptsSlice := make([]Metadata, len(t.Subscripts))
	for i, s := range t.Subscripts {
		subscriptsSlice[i] = d.getOrCreateSubrange(s.Lo, s.Count)
	}
	subscripts, length := llvmMetadataRefs(subscriptsSlice)
	result := C.LLVMDIBuilderCreateArrayType(
		d.ref,
		C.uint64_t(t.SizeInBits),
		C.uint32_t(t.AlignInBits),
		t.ElementType.C,
		subscripts,
		length,
	)
	return Metadata{C: result}
}

// DITypedef holds the values for creating typedef type debug metadata.
type DITypedef struct {
	Type    Metadata
	Name    string
	File    Metadata
	Line    int
	Context Metadata
}

// CreateTypedef creates typedef type debug metadata.
func (d *DIBuilder) CreateTypedef(t DITypedef) Metadata {
	name := C.CString(t.Name)
	defer C.free(unsafe.Pointer(name))
	result := C.LLVMDIBuilderCreateTypedef(
		d.ref,
		t.Type.C,
		name,
		C.size_t(len(t.Name)),
		t.File.C,
		C.unsigned(t.Line),
		t.Context.C,
	)
	return Metadata{C: result}
}

// getOrCreateSubrange gets a metadata node for the specified subrange,
// creating if required.
func (d *DIBuilder) getOrCreateSubrange(lo, count int64) Metadata {
	result := C.LLVMDIBuilderGetOrCreateSubrange(d.ref, C.int64_t(lo), C.int64_t(count))
	return Metadata{C: result}
}

// getOrCreateArray gets a metadata node containing the specified values,
// creating if required.
func (d *DIBuilder) getOrCreateArray(values []Metadata) Metadata {
	if len(values) == 0 {
		return Metadata{}
	}
	data, length := llvmMetadataRefs(values)
	result := C.LLVMDIBuilderGetOrCreateArray(d.ref, data, C.size_t(length))
	return Metadata{C: result}
}

// getOrCreateTypeArray gets a metadata node for a type array containing the
// specified values, creating if required.
func (d *DIBuilder) getOrCreateTypeArray(values []Metadata) Metadata {
	if len(values) == 0 {
		return Metadata{}
	}
	data, length := llvmMetadataRefs(values)
	result := C.LLVMDIBuilderGetOrCreateTypeArray(d.ref, data, C.size_t(length))
	return Metadata{C: result}
}

// CreateExpression creates a new descriptor for the specified
// variable which has a complex address expression for its address.
func (d *DIBuilder) CreateExpression(addr []int64) Metadata {
	var data *C.int64_t
	if len(addr) > 0 {
		data = (*C.int64_t)(unsafe.Pointer(&addr[0]))
	}
	result := C.LLVMDIBuilderCreateExpression(d.ref, data, C.size_t(len(addr)))
	return Metadata{C: result}
}

// InsertDeclareAtEnd inserts a call to llvm.dbg.declare at the end of the
// specified basic block for the given value and associated debug metadata.
func (d *DIBuilder) InsertDeclareAtEnd(v Value, diVarInfo, expr Metadata, l DebugLoc, bb BasicBlock) Value {
	loc := C.LLVMDIBuilderCreateDebugLocation(
		d.m.Context().C, C.uint(l.Line), C.uint(l.Col), l.Scope.C, l.InlinedAt.C)
	result := C.LLVMDIBuilderInsertDeclareAtEnd(d.ref, v.C, diVarInfo.C, expr.C, loc, bb.C)
	return Value{C: result}
}

// InsertValueAtEnd inserts a call to llvm.dbg.value at the end of the
// specified basic block for the given value and associated debug metadata.
func (d *DIBuilder) InsertValueAtEnd(v Value, diVarInfo, expr Metadata, l DebugLoc, bb BasicBlock) Value {
	loc := C.LLVMDIBuilderCreateDebugLocation(
		d.m.Context().C, C.uint(l.Line), C.uint(l.Col), l.Scope.C, l.InlinedAt.C)
	result := C.LLVMDIBuilderInsertDbgValueAtEnd(d.ref, v.C, diVarInfo.C, expr.C, loc, bb.C)
	return Value{C: result}
}

func (v Value) SetSubprogram(sp Metadata) {
	C.LLVMSetSubprogram(v.C, sp.C)
}

func boolToCInt(v bool) C.int {
	if v {
		return 1
	}
	return 0
}

//-------------------------------------------------------------------------
// llvm.Metadata
//-------------------------------------------------------------------------

func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) {
	ptr, nvals := llvmMetadataRefs(mds)
	md.C = C.LLVMTemporaryMDNode(c.C, ptr, C.size_t(nvals))
	return
}

func (md Metadata) ReplaceAllUsesWith(new Metadata) {
	C.LLVMMetadataReplaceAllUsesWith(md.C, new.C)
}

type MetadataKind C.LLVMMetadataKind

const (
	MDStringMetadataKind                     = C.LLVMMDStringMetadataKind
	ConstantAsMetadataMetadataKind           = C.LLVMConstantAsMetadataMetadataKind
	LocalAsMetadataMetadataKind              = C.LLVMLocalAsMetadataMetadataKind
	DistinctMDOperandPlaceholderMetadataKind = C.LLVMDistinctMDOperandPlaceholderMetadataKind
	MDTupleMetadataKind                      = C.LLVMMDTupleMetadataKind
	DILocationMetadataKind                   = C.LLVMDILocationMetadataKind
	DIExpressionMetadataKind                 = C.LLVMDIExpressionMetadataKind
	DIGlobalVariableExpressionMetadataKind   = C.LLVMDIGlobalVariableExpressionMetadataKind
	GenericDINodeMetadataKind                = C.LLVMGenericDINodeMetadataKind
	DISubrangeMetadataKind                   = C.LLVMDISubrangeMetadataKind
	DIEnumeratorMetadataKind                 = C.LLVMDIEnumeratorMetadataKind
	DIBasicTypeMetadataKind                  = C.LLVMDIBasicTypeMetadataKind
	DIDerivedTypeMetadataKind                = C.LLVMDIDerivedTypeMetadataKind
	DICompositeTypeMetadataKind              = C.LLVMDICompositeTypeMetadataKind
	DISubroutineTypeMetadataKind             = C.LLVMDISubroutineTypeMetadataKind
	DIFileMetadataKind                       = C.LLVMDIFileMetadataKind
	DICompileUnitMetadataKind                = C.LLVMDICompileUnitMetadataKind
	DISubprogramMetadataKind                 = C.LLVMDISubprogramMetadataKind
	DILexicalBlockMetadataKind               = C.LLVMDILexicalBlockMetadataKind
	DILexicalBlockFileMetadataKind           = C.LLVMDILexicalBlockFileMetadataKind
	DINamespaceMetadataKind                  = C.LLVMDINamespaceMetadataKind
	DIModuleMetadataKind                     = C.LLVMDIModuleMetadataKind
	DITemplateTypeParameterMetadataKind      = C.LLVMDITemplateTypeParameterMetadataKind
	DITemplateValueParameterMetadataKind     = C.LLVMDITemplateValueParameterMetadataKind
	DIGlobalVariableMetadataKind             = C.LLVMDIGlobalVariableMetadataKind
	DILocalVariableMetadataKind              = C.LLVMDILocalVariableMetadataKind
	DILabelMetadataKind                      = C.LLVMDILabelMetadataKind
	DIObjCPropertyMetadataKind               = C.LLVMDIObjCPropertyMetadataKind
	DIImportedEntityMetadataKind             = C.LLVMDIImportedEntityMetadataKind
	DIMacroMetadataKind                      = C.LLVMDIMacroMetadataKind
	DIMacroFileMetadataKind                  = C.LLVMDIMacroFileMetadataKind
	DICommonBlockMetadataKind                = C.LLVMDICommonBlockMetadataKind
)

// Kind returns the metadata kind.
func (md Metadata) Kind() MetadataKind {
	return MetadataKind(C.LLVMGetMetadataKind(md.C))
}

// FileDirectory returns the directory of a DIFile metadata node.
func (md Metadata) FileDirectory() string {
	var length C.unsigned
	ptr := C.LLVMDIFileGetDirectory(md.C, &length)
	return string(((*[1 << 20]byte)(unsafe.Pointer(ptr)))[:length:length])
}

// FileFilename returns the filename of a DIFile metadata node.
func (md Metadata) FileFilename() string {
	var length C.unsigned
	ptr := C.LLVMDIFileGetFilename(md.C, &length)
	return string(((*[1 << 20]byte)(unsafe.Pointer(ptr)))[:length:length])
}

// FileSource returns the source of a DIFile metadata node.
func (md Metadata) FileSource() string {
	var length C.unsigned
	ptr := C.LLVMDIFileGetSource(md.C, &length)
	return string(((*[1 << 20]byte)(unsafe.Pointer(ptr)))[:length:length])
}

// LocationLine returns the line number of a DILocation.
func (md Metadata) LocationLine() uint {
	return uint(C.LLVMDILocationGetLine(md.C))
}

// LocationColumn returns the column (offset from the start of the line) of a
// DILocation.
func (md Metadata) LocationColumn() uint {
	return uint(C.LLVMDILocationGetColumn(md.C))
}

// LocationScope returns the local scope associated with this debug location.
func (md Metadata) LocationScope() Metadata {
	return Metadata{C.LLVMDILocationGetScope(md.C)}
}

// LocationInlinedAt return the "inline at" location associated with this debug
// location.
func (md Metadata) LocationInlinedAt() Metadata {
	return Metadata{C.LLVMDILocationGetInlinedAt(md.C)}
}

// ScopeFile returns the file (DIFile) of a given scope.
func (md Metadata) ScopeFile() Metadata {
	return Metadata{C.LLVMDIScopeGetFile(md.C)}
}