cgen: fix big IntegerLiteral LL postfix

This commit is contained in:
kbkpbot 2025-09-10 08:14:51 +08:00
parent bae7684276
commit 4f4eea48e8
3 changed files with 17 additions and 18 deletions

View file

@ -268,3 +268,12 @@ fn test_int_max() {
assert int_max(-5, 5) == 5 assert int_max(-5, 5) == 5
assert int_max(5, -5) == 5 assert int_max(5, -5) == 5
} }
fn test_big_int() {
x := i64(2147483647)
if x > -2147483649 && x < 2147483648 {
assert true
} else {
assert false
}
}

View file

@ -3184,13 +3184,6 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
g.write('*') g.write('*')
} }
} }
if expr is ast.IntegerLiteral {
if expected_type in [ast.u64_type, ast.u32_type, ast.u16_type] && expr.val[0] != `-` {
g.expr(expr)
g.write('U')
return
}
}
if (exp_sym.kind == .function && !expected_type.has_option_or_result()) if (exp_sym.kind == .function && !expected_type.has_option_or_result())
|| (g.inside_struct_init && expected_type == ast.voidptr_type || (g.inside_struct_init && expected_type == ast.voidptr_type
&& expected_type != got_type_raw && expr !is ast.StructInit) { && expected_type != got_type_raw && expr !is ast.StructInit) {
@ -3814,6 +3807,10 @@ fn (mut g Gen) expr(node_ ast.Expr) {
g.write2('-0', node.val[3..]) g.write2('-0', node.val[3..])
} else { } else {
g.write(node.val) g.write(node.val)
val_i64 := node.val.i64()
if val_i64 > 2147483647 || val_i64 < -2147483648 {
g.write('LL')
}
} }
} }
ast.IsRefType { ast.IsRefType {
@ -5670,13 +5667,6 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) {
} }
} }
g.expr(node.expr) g.expr(node.expr)
if node.expr is ast.IntegerLiteral {
if node_typ in [ast.u64_type, ast.u32_type, ast.u16_type] {
if !node.expr.val.starts_with('-') {
g.write('U')
}
}
}
g.write('))') g.write('))')
} }
} }

View file

@ -1,7 +1,7 @@
u64 VWEAK abc = ((u64)(1U)); // global u64 VWEAK abc = ((u64)(1)); // global
u64 xyz = ((u64)(2U)); // global u64 xyz = ((u64)(2)); // global
u64 VWEAK weak_1 = ((u64)(4U)); // global u64 VWEAK weak_1 = ((u64)(4)); // global
u64 VWEAK weak_2 = ((u64)(5U)); // global u64 VWEAK weak_2 = ((u64)(5)); // global
VV_LOC int main__a_weak_function(void); VV_LOC int main__a_weak_function(void);
VV_LOC void main__main(void); VV_LOC void main__main(void);