Skip to content

Commit

Permalink
Add getting strings from string constants
Browse files Browse the repository at this point in the history
  • Loading branch information
rj45 committed Aug 16, 2023
1 parent 8e7ec80 commit 41ff177
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package llvm
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "errors"
import (
"errors"
"unsafe"
)

type (
// We use these weird structs here because *Ref types are pointers and
Expand Down Expand Up @@ -910,6 +912,18 @@ func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
return
}

// IsConstantString checks if the constant is an array of i8.
func (v Value) IsConstantString() bool {
return C.LLVMIsConstantString(v.C) != 0
}

// ConstGetAsString will return the string contained in a constant.
func (v Value) ConstGetAsString() string {
length := C.ulong(0)
cstr := C.LLVMGetAsString(v.C, &length)
return C.GoStringN(cstr, C.int(length))
}

// Constant expressions
func (v Value) Opcode() Opcode { return Opcode(C.LLVMGetConstOpcode(v.C)) }
func (v Value) InstructionOpcode() Opcode { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
Expand Down

0 comments on commit 41ff177

Please sign in to comment.