diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index 4b3e3ee8a7e2eb..b4a52d4a171666 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -125,6 +125,10 @@ pub fn (n int) str() string { return n.str_l(12) } +pub fn (n i32) str() string { + return int(n).str_l(12) +} + // str returns the value of the `int` as a `string`. // Example: assert int(-2020).str() == '-2020' /* diff --git a/vlib/encoding/leb128/leb128.v b/vlib/encoding/leb128/leb128.v index b5acd5ac19270d..11379a29ca487c 100644 --- a/vlib/encoding/leb128/leb128.v +++ b/vlib/encoding/leb128/leb128.v @@ -80,7 +80,7 @@ pub fn decode_i32(value []u8) (i32, int) { break } } - return int(result), shift / 7 + return i32(result), shift / 7 } // decode_i64 decodes an i64 and returns the number of bytes used from the given leb128 encoded array `value` diff --git a/vlib/math/const.v b/vlib/math/const.v index b2bdf51112a0e3..a69a890176b5ab 100644 --- a/vlib/math/const.v +++ b/vlib/math/const.v @@ -46,8 +46,8 @@ pub const ( max_i8 = i8(127) min_i16 = i16(-32768) max_i16 = i16(32767) - min_i32 = int(-2147483648) - max_i32 = int(2147483647) + min_i32 = i32(-2147483648) + max_i32 = i32(2147483647) // -9223372036854775808 is wrong, because C compilers parse literal values // without sign first, and 9223372036854775808 overflows i64, hence the // consecutive subtraction by 1 diff --git a/vlib/math/limit.v b/vlib/math/limit.v index c752e40737fef2..831a9e9c08e2f5 100644 --- a/vlib/math/limit.v +++ b/vlib/math/limit.v @@ -10,7 +10,7 @@ pub fn maxof[T]() T { return max_i8 } $else $if T is i16 { return max_i16 - } $else $if T is int { + } $else $if T is i32 { return max_i32 } $else $if T is i32 { return max_i32 @@ -30,6 +30,11 @@ pub fn maxof[T]() T { return max_f32 } $else $if T is f64 { return max_f64 + } $else $if T is int { + $if new_int ? { + return int(max_i64) + } + return int(max_i32) } $else { panic('A maximum value of the type `${typeof[T]().name}` is not defined.') } @@ -42,7 +47,7 @@ pub fn minof[T]() T { return min_i8 } $else $if T is i16 { return min_i16 - } $else $if T is int { + } $else $if T is i32 { return min_i32 } $else $if T is i32 { return min_i32 @@ -62,6 +67,11 @@ pub fn minof[T]() T { return -max_f32 } $else $if T is f64 { return -max_f64 + } $else $if T is int { + $if new_int ? { + return int(min_i64) + } + return int(min_i32) } $else { panic('A minimum value of the type `${typeof[T]().name}` is not defined.') } diff --git a/vlib/math/log.v b/vlib/math/log.v index 1d3d5f0a9c6578..b01e0ee4778b01 100644 --- a/vlib/math/log.v +++ b/vlib/math/log.v @@ -59,13 +59,13 @@ pub fn log_b(x f64) f64 { // ilog_b(nan) = max_i32 pub fn ilog_b(x f64) int { if x == 0 { - return min_i32 + return int(min_i32) } if is_nan(x) { - return max_i32 + return int(max_i32) } if is_inf(x, 0) { - return max_i32 + return int(max_i32) } return ilog_b_(x) } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 5006b738ccfd70..ad6f49fc4fa777 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -9,9 +9,10 @@ import v.pref pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl -pub const int_type_name = $if amd64 || arm64 { - 'int' - //'i64' +// pub const int_type_name = $if amd64 || arm64 { +pub const int_type_name = $if new_int ? { + //'int' + 'i64' } $else { 'int' } diff --git a/vlib/v/ast/comptime_const_values.v b/vlib/v/ast/comptime_const_values.v index 4780b2a2178223..c9ca312f7faec6 100644 --- a/vlib/v/ast/comptime_const_values.v +++ b/vlib/v/ast/comptime_const_values.v @@ -6,7 +6,7 @@ pub type ComptTimeConstValue = EmptyExpr | i16 | i64 | i8 - | int + | i32 | rune | string | u16 @@ -14,6 +14,7 @@ pub type ComptTimeConstValue = EmptyExpr | u64 | u8 | voidptr + //| int pub fn empty_comptime_const_expr() ComptTimeConstValue { return EmptyExpr(0) @@ -43,9 +44,17 @@ pub fn (val ComptTimeConstValue) int() ?int { return none } +pub fn (val ComptTimeConstValue) i32() ?i32 { + x := val.i64()? + if x > -2147483649 && x < 2147483648 { + return i32(x) + } + return none +} + pub fn (val ComptTimeConstValue) voidptr() ?voidptr { match val { - i8, i16, int, i64 { return voidptr(i64(val)) } + i8, i16, i32, i64 { return voidptr(i64(val)) } u8, u16, u32, u64 { return voidptr(u64(val)) } rune { return voidptr(u64(val)) } voidptr { return val } @@ -62,7 +71,7 @@ pub fn (val ComptTimeConstValue) i64() ?i64 { i16 { return i64(val) } - int { + i32 { return i64(val) } i64 { @@ -144,7 +153,7 @@ pub fn (val ComptTimeConstValue) u64() ?u64 { return u64(val) } } - int { + i32 { if val >= 0 { return u64(val) } @@ -201,7 +210,7 @@ pub fn (val ComptTimeConstValue) f64() ?f64 { i16 { return f64(val) } - int { + i32 { return f64(val) } i64 { @@ -243,7 +252,7 @@ pub fn (val ComptTimeConstValue) string() ?string { i16 { return val.str() } - int { + i32 { return val.str() } i64 { diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 4449cb28225345..cefc2c1989a5f2 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -324,7 +324,7 @@ fn (mut c Checker) eval_comptime_const_expr(expr ast.Expr, nlevel int) ?ast.Comp } ast.SizeOf { s, _ := c.table.type_size(expr.typ) - return s + return i64(s) } ast.FloatLiteral { x := expr.val.f64() @@ -361,8 +361,8 @@ fn (mut c Checker) eval_comptime_const_expr(expr ast.Expr, nlevel int) ?ast.Comp if expr.typ == ast.i16_type { return cast_expr_value.i16() or { return none } } - if expr.typ == ast.int_type { - return cast_expr_value.int() or { return none } + if expr.typ == ast.i32_type { + return cast_expr_value.i32() or { return none } } if expr.typ == ast.i64_type { return cast_expr_value.i64() or { return none } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 20037e70b058ac..2791c0b4ad41c0 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5387,7 +5387,7 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, field_name string i16 { g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str()) } - int { + i32 { g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str()) } i64 {