1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: make u8 the primary type, byte the alias

This commit is contained in:
Alexander Medvednikov 2022-04-15 13:43:03 +03:00
parent 566f150b24
commit e6ff1508d2
7 changed files with 45 additions and 44 deletions

View File

@ -172,9 +172,9 @@ pub fn (a array) repeat_to_depth(count int, depth int) array {
for i in 0 .. count { for i in 0 .. count {
if depth > 0 { if depth > 0 {
ary_clone := unsafe { a.clone_to_depth(depth) } ary_clone := unsafe { a.clone_to_depth(depth) }
unsafe { vmemcpy(arr.get_unsafe(i * a.len), &byte(ary_clone.data), a.len * a.element_size) } unsafe { vmemcpy(arr.get_unsafe(i * a.len), &u8(ary_clone.data), a.len * a.element_size) }
} else { } else {
unsafe { vmemcpy(arr.get_unsafe(i * a.len), &byte(a.data), a.len * a.element_size) } unsafe { vmemcpy(arr.get_unsafe(i * a.len), &u8(a.data), a.len * a.element_size) }
} }
} }
} }
@ -286,7 +286,7 @@ pub fn (mut a array) delete_many(i int, size int) {
} }
if a.flags.all(.noshrink | .noslices) { if a.flags.all(.noshrink | .noslices) {
unsafe { unsafe {
vmemmove(&byte(a.data) + i * a.element_size, &byte(a.data) + (i + size) * a.element_size, vmemmove(&u8(a.data) + i * a.element_size, &u8(a.data) + (i + size) * a.element_size,
(a.len - i - size) * a.element_size) (a.len - i - size) * a.element_size)
} }
a.len -= size a.len -= size
@ -300,7 +300,7 @@ pub fn (mut a array) delete_many(i int, size int) {
a.data = vcalloc(new_cap * a.element_size) a.data = vcalloc(new_cap * a.element_size)
unsafe { vmemcpy(a.data, old_data, i * a.element_size) } unsafe { vmemcpy(a.data, old_data, i * a.element_size) }
unsafe { unsafe {
vmemcpy(&byte(a.data) + i * a.element_size, &byte(old_data) + (i + size) * a.element_size, vmemcpy(&u8(a.data) + i * a.element_size, &u8(old_data) + (i + size) * a.element_size,
(a.len - i - size) * a.element_size) (a.len - i - size) * a.element_size)
} }
if a.flags.has(.noslices) { if a.flags.has(.noslices) {
@ -344,7 +344,7 @@ pub fn (mut a array) drop(num int) {
} }
n := if num <= a.len { num } else { a.len } n := if num <= a.len { num } else { a.len }
blen := n * a.element_size blen := n * a.element_size
a.data = unsafe { &byte(a.data) + blen } a.data = unsafe { &u8(a.data) + blen }
a.offset += blen a.offset += blen
a.len -= n a.len -= n
a.cap -= n a.cap -= n
@ -354,7 +354,7 @@ pub fn (mut a array) drop(num int) {
[inline; unsafe] [inline; unsafe]
fn (a array) get_unsafe(i int) voidptr { fn (a array) get_unsafe(i int) voidptr {
unsafe { unsafe {
return &byte(a.data) + i * a.element_size return &u8(a.data) + i * a.element_size
} }
} }
@ -366,7 +366,7 @@ fn (a array) get(i int) voidptr {
} }
} }
unsafe { unsafe {
return &byte(a.data) + i * a.element_size return &u8(a.data) + i * a.element_size
} }
} }
@ -376,7 +376,7 @@ fn (a array) get_with_check(i int) voidptr {
return 0 return 0
} }
unsafe { unsafe {
return &byte(a.data) + i * a.element_size return &u8(a.data) + i * a.element_size
} }
} }
@ -402,7 +402,7 @@ pub fn (a array) last() voidptr {
} }
} }
unsafe { unsafe {
return &byte(a.data) + (a.len - 1) * a.element_size return &u8(a.data) + (a.len - 1) * a.element_size
} }
} }
@ -429,7 +429,7 @@ pub fn (mut a array) pop() voidptr {
} }
} }
new_len := a.len - 1 new_len := a.len - 1
last_elem := unsafe { &byte(a.data) + new_len * a.element_size } last_elem := unsafe { &u8(a.data) + new_len * a.element_size }
a.len = new_len a.len = new_len
// Note: a.cap is not changed here *on purpose*, so that // Note: a.cap is not changed here *on purpose*, so that
// further << ops on that array will be more efficient. // further << ops on that array will be more efficient.
@ -475,7 +475,7 @@ fn (a array) slice(start int, _end int) array {
// TODO: integrate reference counting // TODO: integrate reference counting
// a.flags.clear(.noslices) // a.flags.clear(.noslices)
offset := start * a.element_size offset := start * a.element_size
data := unsafe { &byte(a.data) + offset } data := unsafe { &u8(a.data) + offset }
l := end - start l := end - start
res := array{ res := array{
element_size: a.element_size element_size: a.element_size
@ -527,7 +527,7 @@ fn (a array) slice_ni(_start int, _end int) array {
} }
offset := start * a.element_size offset := start * a.element_size
data := unsafe { &byte(a.data) + offset } data := unsafe { &u8(a.data) + offset }
l := end - start l := end - start
res := array{ res := array{
element_size: a.element_size element_size: a.element_size
@ -583,7 +583,7 @@ pub fn (a &array) clone_to_depth(depth int) array {
return arr return arr
} else { } else {
if !isnil(a.data) { if !isnil(a.data) {
unsafe { vmemcpy(&byte(arr.data), a.data, a.cap * a.element_size) } unsafe { vmemcpy(&u8(arr.data), a.data, a.cap * a.element_size) }
} }
return arr return arr
} }
@ -592,7 +592,7 @@ pub fn (a &array) clone_to_depth(depth int) array {
// we manually inline this for single operations for performance without -prod // we manually inline this for single operations for performance without -prod
[inline; unsafe] [inline; unsafe]
fn (mut a array) set_unsafe(i int, val voidptr) { fn (mut a array) set_unsafe(i int, val voidptr) {
unsafe { vmemcpy(&byte(a.data) + a.element_size * i, val, a.element_size) } unsafe { vmemcpy(&u8(a.data) + a.element_size * i, val, a.element_size) }
} }
// Private function. Used to implement assigment to the array element. // Private function. Used to implement assigment to the array element.
@ -602,14 +602,14 @@ fn (mut a array) set(i int, val voidptr) {
panic('array.set: index out of range (i == $i, a.len == $a.len)') panic('array.set: index out of range (i == $i, a.len == $a.len)')
} }
} }
unsafe { vmemcpy(&byte(a.data) + a.element_size * i, val, a.element_size) } unsafe { vmemcpy(&u8(a.data) + a.element_size * i, val, a.element_size) }
} }
fn (mut a array) push(val voidptr) { fn (mut a array) push(val voidptr) {
if a.len >= a.cap { if a.len >= a.cap {
a.ensure_cap(a.len + 1) a.ensure_cap(a.len + 1)
} }
unsafe { vmemcpy(&byte(a.data) + a.element_size * a.len, val, a.element_size) } unsafe { vmemcpy(&u8(a.data) + a.element_size * a.len, val, a.element_size) }
a.len++ a.len++
} }
@ -641,10 +641,10 @@ pub fn (mut a array) reverse_in_place() {
unsafe { unsafe {
mut tmp_value := malloc(a.element_size) mut tmp_value := malloc(a.element_size)
for i in 0 .. a.len / 2 { for i in 0 .. a.len / 2 {
vmemcpy(tmp_value, &byte(a.data) + i * a.element_size, a.element_size) vmemcpy(tmp_value, &u8(a.data) + i * a.element_size, a.element_size)
vmemcpy(&byte(a.data) + i * a.element_size, &byte(a.data) + vmemcpy(&u8(a.data) + i * a.element_size, &u8(a.data) +
(a.len - 1 - i) * a.element_size, a.element_size) (a.len - 1 - i) * a.element_size, a.element_size)
vmemcpy(&byte(a.data) + (a.len - 1 - i) * a.element_size, tmp_value, a.element_size) vmemcpy(&u8(a.data) + (a.len - 1 - i) * a.element_size, tmp_value, a.element_size)
} }
free(tmp_value) free(tmp_value)
} }
@ -676,7 +676,7 @@ pub fn (a &array) free() {
// if a.is_slice { // if a.is_slice {
// return // return
// } // }
mblock_ptr := &byte(u64(a.data) - u64(a.offset)) mblock_ptr := &u8(u64(a.data) - u64(a.offset))
unsafe { free(mblock_ptr) } unsafe { free(mblock_ptr) }
} }
@ -845,12 +845,12 @@ pub fn (b []byte) hex() string {
for i in b { for i in b {
n0 := i >> 4 n0 := i >> 4
unsafe { unsafe {
hex[dst_i] = if n0 < 10 { n0 + `0` } else { n0 + byte(87) } hex[dst_i] = if n0 < 10 { n0 + `0` } else { n0 + u8(87) }
dst_i++ dst_i++
} }
n1 := i & 0xF n1 := i & 0xF
unsafe { unsafe {
hex[dst_i] = if n1 < 10 { n1 + `0` } else { n1 + byte(87) } hex[dst_i] = if n1 < 10 { n1 + `0` } else { n1 + u8(87) }
dst_i++ dst_i++
} }
} }
@ -868,7 +868,7 @@ pub fn (b []byte) hex() string {
pub fn copy(mut dst []byte, src []byte) int { pub fn copy(mut dst []byte, src []byte) int {
min := if dst.len < src.len { dst.len } else { src.len } min := if dst.len < src.len { dst.len } else { src.len }
if min > 0 { if min > 0 {
unsafe { vmemmove(&byte(dst.data), src.data, min) } unsafe { vmemmove(&u8(dst.data), src.data, min) }
} }
return min return min
} }

View File

@ -111,7 +111,7 @@ pub fn c_error_number_str(errnum int) string {
$if !vinix { $if !vinix {
c_msg := C.strerror(errnum) c_msg := C.strerror(errnum)
err_msg = string{ err_msg = string{
str: &byte(c_msg) str: &u8(c_msg)
len: unsafe { C.strlen(c_msg) } len: unsafe { C.strlen(c_msg) }
is_lit: 1 is_lit: 1
} }
@ -284,7 +284,7 @@ pub fn malloc(n int) &byte {
C.fprintf(C.stderr, c'_v_malloc %6d total %10d\n', n, total_m) C.fprintf(C.stderr, c'_v_malloc %6d total %10d\n', n, total_m)
// print_backtrace() // print_backtrace()
} }
mut res := &byte(0) mut res := &u8(0)
$if prealloc { $if prealloc {
return unsafe { prealloc_malloc(n) } return unsafe { prealloc_malloc(n) }
} $else $if gcboehm ? { } $else $if gcboehm ? {
@ -327,7 +327,7 @@ pub fn malloc_noscan(n int) &byte {
C.fprintf(C.stderr, c'malloc_noscan %6d total %10d\n', n, total_m) C.fprintf(C.stderr, c'malloc_noscan %6d total %10d\n', n, total_m)
// print_backtrace() // print_backtrace()
} }
mut res := &byte(0) mut res := &u8(0)
$if prealloc { $if prealloc {
return unsafe { prealloc_malloc(n) } return unsafe { prealloc_malloc(n) }
} $else $if gcboehm ? { } $else $if gcboehm ? {
@ -365,7 +365,7 @@ pub fn v_realloc(b &byte, n int) &byte {
$if trace_realloc ? { $if trace_realloc ? {
C.fprintf(C.stderr, c'v_realloc %6d\n', n) C.fprintf(C.stderr, c'v_realloc %6d\n', n)
} }
mut new_ptr := &byte(0) mut new_ptr := &u8(0)
$if prealloc { $if prealloc {
unsafe { unsafe {
new_ptr = malloc(n) new_ptr = malloc(n)
@ -417,7 +417,7 @@ pub fn realloc_data(old_data &byte, old_size int, new_size int) &byte {
return new_ptr return new_ptr
} }
} }
mut nptr := &byte(0) mut nptr := &u8(0)
$if gcboehm ? { $if gcboehm ? {
nptr = unsafe { C.GC_REALLOC(old_data, new_size) } nptr = unsafe { C.GC_REALLOC(old_data, new_size) }
} $else { } $else {
@ -436,7 +436,7 @@ pub fn vcalloc(n int) &byte {
if n < 0 { if n < 0 {
panic('calloc($n < 0)') panic('calloc($n < 0)')
} else if n == 0 { } else if n == 0 {
return &byte(0) return &u8(0)
} }
$if trace_vcalloc ? { $if trace_vcalloc ? {
total_m += n total_m += n

View File

@ -7,7 +7,8 @@ module builtin
// ----- value to string functions ----- // ----- value to string functions -----
// //
type u8 = byte //type u8 = byte
type byte = u8
type i32 = int type i32 = int
// ptr_str returns the address of `ptr` as a `string`. // ptr_str returns the address of `ptr` as a `string`.

View File

@ -1,7 +1,7 @@
module ast module ast
pub type ComptTimeConstValue = EmptyExpr pub type ComptTimeConstValue = EmptyExpr
| byte | u8
| f32 | f32
| f64 | f64
| i16 | i16
@ -56,9 +56,9 @@ pub fn (val ComptTimeConstValue) i64() ?i64 {
i64 { i64 {
return i64(val) return i64(val)
} }
//
byte { u8 {
return i64(val) return i64(val)
} }
u16 { u16 {
return i64(val) return i64(val)
@ -94,10 +94,10 @@ pub fn (val ComptTimeConstValue) i64() ?i64 {
return none return none
} }
pub fn (val ComptTimeConstValue) byte() ?byte { pub fn (val ComptTimeConstValue) u8() ?u8 {
x := val.u64() ? x := val.u64() ?
if x < 256 { if x < 256 {
return byte(x) return u8(x)
} }
return none return none
} }
@ -140,7 +140,7 @@ pub fn (val ComptTimeConstValue) u64() ?u64 {
return u64(val) return u64(val)
} }
} }
byte { u8 {
return u64(val) return u64(val)
} }
u16 { u16 {
@ -190,7 +190,7 @@ pub fn (val ComptTimeConstValue) f64() ?f64 {
i64 { i64 {
return f64(val) return f64(val)
} }
byte { u8 {
return f64(val) return f64(val)
} }
u16 { u16 {
@ -231,7 +231,7 @@ pub fn (val ComptTimeConstValue) string() ?string {
i64 { i64 {
return val.str() return val.str()
} }
byte { u8 {
return val.str() return val.str()
} }
u16 { u16 {

View File

@ -446,7 +446,7 @@ pub const (
// Note: builtin_type_names must be in the same order as the idx consts above // Note: builtin_type_names must be in the same order as the idx consts above
pub const builtin_type_names = ['void', 'voidptr', 'byteptr', 'charptr', 'i8', 'i16', 'int', 'i64', pub const builtin_type_names = ['void', 'voidptr', 'byteptr', 'charptr', 'i8', 'i16', 'int', 'i64',
'isize', 'byte', 'u16', 'u32', 'u64', 'usize', 'f32', 'f64', 'char', 'bool', 'none', 'string', 'isize', 'u8', 'u16', 'u32', 'u64', 'usize', 'f32', 'f64', 'char', 'bool', 'none', 'string',
'rune', 'array', 'map', 'chan', 'any', 'float_literal', 'int_literal', 'thread', 'Error', 'u8'] 'rune', 'array', 'map', 'chan', 'any', 'float_literal', 'int_literal', 'thread', 'Error', 'u8']
pub const builtin_type_names_matcher = build_builtin_type_names_matcher() pub const builtin_type_names_matcher = build_builtin_type_names_matcher()
@ -745,7 +745,7 @@ pub fn (mut t Table) register_builtin_type_symbols() {
t.register_sym(kind: .int, name: 'int', cname: 'int', mod: 'builtin') t.register_sym(kind: .int, name: 'int', cname: 'int', mod: 'builtin')
t.register_sym(kind: .i64, name: 'i64', cname: 'i64', mod: 'builtin') t.register_sym(kind: .i64, name: 'i64', cname: 'i64', mod: 'builtin')
t.register_sym(kind: .isize, name: 'isize', cname: 'isize', mod: 'builtin') t.register_sym(kind: .isize, name: 'isize', cname: 'isize', mod: 'builtin')
t.register_sym(kind: .byte, name: 'byte', cname: 'byte', mod: 'builtin') t.register_sym(kind: .byte, name: 'u8', cname: 'u8', mod: 'builtin')
t.register_sym(kind: .u16, name: 'u16', cname: 'u16', mod: 'builtin') t.register_sym(kind: .u16, name: 'u16', cname: 'u16', mod: 'builtin')
t.register_sym(kind: .u32, name: 'u32', cname: 'u32', mod: 'builtin') t.register_sym(kind: .u32, name: 'u32', cname: 'u32', mod: 'builtin')
t.register_sym(kind: .u64, name: 'u64', cname: 'u64', mod: 'builtin') t.register_sym(kind: .u64, name: 'u64', cname: 'u64', mod: 'builtin')

View File

@ -195,7 +195,7 @@ fn (mut c Checker) eval_comptime_const_expr(expr ast.Expr, nlevel int) ?ast.Comp
} }
// //
if expr.typ == ast.byte_type { if expr.typ == ast.byte_type {
return cast_expr_value.byte() or { return none } return cast_expr_value.u8() or { return none }
} }
if expr.typ == ast.u16_type { if expr.typ == ast.u16_type {
return cast_expr_value.u16() or { return none } return cast_expr_value.u16() or { return none }

View File

@ -966,7 +966,7 @@ fn (mut g Gen) optional_type_name(t ast.Type) (string, string) {
fn (g Gen) optional_type_text(styp string, base string) string { fn (g Gen) optional_type_text(styp string, base string) string {
// replace void with something else // replace void with something else
size := if base == 'void' { size := if base == 'void' {
'byte' 'u8'
} else if base.starts_with('anon_fn') { } else if base.starts_with('anon_fn') {
'void*' 'void*'
} else { } else {
@ -4280,7 +4280,7 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, ct_value ast.Comp
g.const_decl_write_precomputed(styp, cname, ct_value.str()) g.const_decl_write_precomputed(styp, cname, ct_value.str())
} }
} }
byte { u8 {
g.const_decl_write_precomputed(styp, cname, ct_value.str()) g.const_decl_write_precomputed(styp, cname, ct_value.str())
} }
u16 { u16 {