compiler: implement []byte(str)

Этот коммит содержится в:
Ayke van Laethem 2018-09-03 00:21:33 +02:00
родитель a080ce26ef
коммит ebd87ce4cd
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
2 изменённых файлов: 36 добавлений и 6 удалений

Просмотреть файл

@ -2414,6 +2414,20 @@ func (c *Compiler) parseConvert(typeFrom, typeTo types.Type, value llvm.Value) (
return llvm.Value{}, errors.New("todo: convert: basic non-integer type: " + typeFrom.String() + " -> " + typeTo.String())
case *types.Slice:
if basic, ok := typeFrom.(*types.Basic); !ok || basic.Kind() != types.String {
panic("can only convert from a string to a slice")
}
elemType := typeTo.Elem().Underlying().(*types.Basic) // must be byte or rune
switch elemType.Kind() {
case types.Byte:
fn := c.mod.NamedFunction("runtime.stringToBytes")
return c.builder.CreateCall(fn, []llvm.Value{value}, ""), nil
default:
return llvm.Value{}, errors.New("todo: convert from string: " + elemType.String())
}
default:
return llvm.Value{}, errors.New("todo: convert " + typeTo.String() + " <- " + typeFrom.String())
}

Просмотреть файл

@ -42,12 +42,28 @@ func stringConcat(x, y _string) _string {
}
// Create a string from a []byte slice.
func stringFromBytes(x []byte) _string {
buf := alloc(uintptr(len(x)))
for i, c := range x {
*(*byte)(unsafe.Pointer(uintptr(buf) + uintptr(i))) = c
func stringFromBytes(x struct {
ptr *byte
len lenType
cap lenType
}) _string {
buf := alloc(uintptr(x.len))
memcpy(buf, unsafe.Pointer(x.ptr), uintptr(x.len))
return _string{lenType(x.len), (*byte)(buf)}
}
return _string{lenType(len(x)), (*byte)(buf)}
// Convert a string to a []byte slice.
func stringToBytes(x _string) (slice struct {
ptr *byte
len lenType
cap lenType
}) {
buf := alloc(uintptr(x.length))
memcpy(buf, unsafe.Pointer(x.ptr), uintptr(x.length))
slice.ptr = (*byte)(buf)
slice.len = x.length
slice.cap = x.length
return
}
// Create a string from a Unicode code point.