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

check integer const overflow at compilation

This commit is contained in:
Alexander Medvednikov
2019-07-10 16:05:39 +02:00
parent 69d2db0f1e
commit 3d4cd0bbc0
4 changed files with 45 additions and 2 deletions

View File

@ -4,6 +4,8 @@
module main
import math
struct Table {
mut:
types []Type
@ -637,3 +639,21 @@ fn (table &Table) cgen_name_type_pair(name, typ string) string {
return '$typ $name'
}
fn is_valid_int_const(val, typ string) bool {
x := val.int()
switch typ {
case 'byte', 'u8': return 0 <= x && x <= math.MaxU8
case 'u16': return 0 <= x && x <= math.MaxU16
case 'u32': return 0 <= x && x <= math.MaxU32
//case 'u64': return 0 <= x && x <= math.MaxU64
//////////////
case 'i8': return math.MinI8 <= x && x <= math.MaxI8
case 'i16': return math.MinI16 <= x && x <= math.MaxI16
case 'int', 'i32': return math.MinI32 <= x && x <= math.MaxI32
//case 'i64':
//x64 := val.i64()
//return i64(-(1<<63)) <= x64 && x64 <= i64((1<<63)-1)
}
return true
}