Skip to content

Commit de90b5e

Browse files
committed
v2: lower a lot more nodes in transformer: CallOrCastExpr, RangeExpr, FieldInit; remove this logic from cleanc
1 parent 9ad47f9 commit de90b5e

3 files changed

Lines changed: 511 additions & 260 deletions

File tree

‎vlib/v2/gen/cleanc/cleancnew.v‎

Lines changed: 11 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -5547,46 +5547,7 @@ fn (mut g Gen) gen_expr(node ast.Expr) {
55475547
g.gen_call_expr(node.lhs, node.args)
55485548
}
55495549
ast.CallOrCastExpr {
5550-
// Check if this is a type cast: int(x), MyInt(42), etc.
5551-
if node.lhs is ast.Ident && g.is_type_name(node.lhs.name) {
5552-
type_name := g.expr_type_to_c(node.lhs)
5553-
g.gen_type_cast_expr(type_name, node.expr)
5554-
} else if node.lhs is ast.Type {
5555-
type_name := g.expr_type_to_c(node.lhs)
5556-
g.gen_type_cast_expr(type_name, node.expr)
5557-
} else if node.lhs is ast.SelectorExpr {
5558-
sel := node.lhs as ast.SelectorExpr
5559-
if sel.lhs is ast.Ident && sel.lhs.name == 'C' && g.is_c_type_name(sel.rhs.name) {
5560-
// C.TYPE(expr) is a type cast to a C type
5561-
type_name := g.expr_type_to_c(node.lhs)
5562-
g.gen_type_cast_expr(type_name, node.expr)
5563-
} else if sel.lhs is ast.Ident {
5564-
// module.Type(expr) - check if this is a type cast
5565-
type_name := g.expr_type_to_c(node.lhs)
5566-
if g.is_type_name(type_name) {
5567-
g.gen_type_cast_expr(type_name, node.expr)
5568-
} else {
5569-
mut call_args := []ast.Expr{cap: 1}
5570-
if node.expr !is ast.EmptyExpr {
5571-
call_args << node.expr
5572-
}
5573-
g.gen_call_expr(node.lhs, call_args)
5574-
}
5575-
} else {
5576-
mut call_args := []ast.Expr{cap: 1}
5577-
if node.expr !is ast.EmptyExpr {
5578-
call_args << node.expr
5579-
}
5580-
g.gen_call_expr(node.lhs, call_args)
5581-
}
5582-
} else {
5583-
// Single-arg call: println(x) is parsed as CallOrCastExpr
5584-
mut call_args := []ast.Expr{cap: 1}
5585-
if node.expr !is ast.EmptyExpr {
5586-
call_args << node.expr
5587-
}
5588-
g.gen_call_expr(node.lhs, call_args)
5589-
}
5550+
panic('bug in v2 compiler: CallOrCastExpr should have been lowered in v2.transformer')
55905551
}
55915552
ast.SelectorExpr {
55925553
// typeof(x).name -> just emit the typeof string directly (already a string)
@@ -5856,7 +5817,7 @@ fn (mut g Gen) gen_expr(node ast.Expr) {
58565817
g.gen_keyword_operator(node)
58575818
}
58585819
ast.RangeExpr {
5859-
g.gen_range_expr(node)
5820+
panic('bug in v2 compiler: RangeExpr should have been lowered in v2.transformer')
58605821
}
58615822
ast.SelectExpr {
58625823
g.sb.write_string('/* [TODO] SelectExpr */ 0')
@@ -5883,9 +5844,7 @@ fn (mut g Gen) gen_expr(node ast.Expr) {
58835844
g.sb.write_string('})')
58845845
}
58855846
ast.FieldInit {
5886-
// FieldInit should be grouped into InitExpr by call argument lowering.
5887-
// Fallback to value expression to keep C output valid.
5888-
g.gen_expr(node.value)
5847+
panic('bug in v2 compiler: FieldInit in expression position should have been lowered in v2.transformer')
58895848
}
58905849
ast.IfGuardExpr {
58915850
panic('bug in v2 compiler: IfGuardExpr should have been expanded in v2.transformer')
@@ -6757,76 +6716,6 @@ fn (mut g Gen) get_call_return_type(lhs ast.Expr, arg_count int) ?string {
67576716
return none
67586717
}
67596718

6760-
fn (mut g Gen) normalize_named_call_args(fn_name string, call_args []ast.Expr) []ast.Expr {
6761-
_ = fn_name
6762-
mut out := []ast.Expr{cap: call_args.len}
6763-
out << call_args
6764-
return out
6765-
}
6766-
6767-
fn call_args_have_field_init(call_args []ast.Expr) bool {
6768-
for arg in call_args {
6769-
if arg is ast.FieldInit {
6770-
return true
6771-
}
6772-
}
6773-
return false
6774-
}
6775-
6776-
fn (mut g Gen) gen_named_struct_call_arg(fn_name string, param_idx int, call_args []ast.Expr, arg_idx int) int {
6777-
if arg_idx >= call_args.len || call_args[arg_idx] !is ast.FieldInit {
6778-
return -1
6779-
}
6780-
param_types := g.fn_param_types[fn_name] or { return -1 }
6781-
if param_idx >= param_types.len {
6782-
return -1
6783-
}
6784-
raw_param_type := unmangle_c_ptr_type(param_types[param_idx])
6785-
param_type := raw_param_type.trim_right('*')
6786-
if param_type == '' || param_type in primitive_types || param_type.starts_with('Array_')
6787-
|| param_type.starts_with('Map_') || param_type.starts_with('_option_')
6788-
|| param_type.starts_with('_result_') {
6789-
return -1
6790-
}
6791-
mut fields := []ast.FieldInit{}
6792-
mut j := arg_idx
6793-
for j < call_args.len {
6794-
arg := call_args[j]
6795-
if arg is ast.FieldInit {
6796-
fields << arg
6797-
j++
6798-
continue
6799-
}
6800-
break
6801-
}
6802-
if fields.len == 0 {
6803-
return -1
6804-
}
6805-
is_ptr_param := if ptrs := g.fn_param_is_ptr[fn_name] {
6806-
param_idx < ptrs.len && ptrs[param_idx]
6807-
} else {
6808-
raw_param_type.ends_with('*')
6809-
}
6810-
if is_ptr_param {
6811-
g.sb.write_string('&')
6812-
}
6813-
g.sb.write_string('((${param_type}){')
6814-
for i, field in fields {
6815-
if i > 0 {
6816-
g.sb.write_string(', ')
6817-
}
6818-
field_name := if field.name.contains('.') {
6819-
field.name
6820-
} else {
6821-
escape_c_keyword(field.name)
6822-
}
6823-
g.sb.write_string('.${field_name} = ')
6824-
g.gen_expr(field.value)
6825-
}
6826-
g.sb.write_string('})')
6827-
return j
6828-
}
6829-
68306719
fn (mut g Gen) gen_type_cast_expr(type_name string, expr ast.Expr) {
68316720
expr_type := g.get_expr_type(expr)
68326721
if expr_type.starts_with('_result_') && g.result_value_type(expr_type) != '' {
@@ -7459,7 +7348,11 @@ fn (mut g Gen) gen_call_expr(lhs ast.Expr, args []ast.Expr) {
74597348
}
74607349
}
74617350
}
7462-
call_args = g.normalize_named_call_args(name, call_args)
7351+
for arg in call_args {
7352+
if arg is ast.FieldInit {
7353+
panic('bug in v2 compiler: FieldInit call args should have been lowered in v2.transformer')
7354+
}
7355+
}
74637356
if name != '' {
74647357
g.called_fn_names[name] = true
74657358
}
@@ -7773,7 +7666,6 @@ fn (mut g Gen) gen_call_expr(lhs ast.Expr, args []ast.Expr) {
77737666
if c_name in ['os__exit', 'builder__exit'] {
77747667
c_name = 'exit'
77757668
}
7776-
call_args = g.normalize_named_call_args(c_name, call_args)
77777669
if c_name == 'array__push_many' && call_args.len == 3 && call_args[1] is ast.SelectorExpr
77787670
&& call_args[2] is ast.SelectorExpr {
77797671
data_sel := call_args[1] as ast.SelectorExpr
@@ -7879,36 +7771,23 @@ fn (mut g Gen) gen_call_expr(lhs ast.Expr, args []ast.Expr) {
78797771
if param_types.len > total_args {
78807772
total_args = param_types.len
78817773
}
7882-
// Named call arguments are lowered as repeated FieldInit entries.
7883-
// Use the declared parameter count so grouped fields map to one arg.
7884-
if call_args_have_field_init(call_args) && param_types.len > 0 {
7885-
total_args = param_types.len
7886-
}
78877774
} else if params := g.fn_param_is_ptr[c_name] {
78887775
if params.len > total_args {
78897776
total_args = params.len
78907777
}
78917778
}
7892-
mut arg_idx := 0
78937779
for i in 0 .. total_args {
78947780
if i > 0 {
78957781
g.sb.write_string(', ')
78967782
}
7897-
if arg_idx < call_args.len {
7898-
next_idx := g.gen_named_struct_call_arg(c_name, i, call_args, arg_idx)
7899-
if next_idx >= 0 {
7900-
arg_idx = next_idx
7901-
continue
7902-
}
7783+
if i < call_args.len {
79037784
if c_name == 'signal' && i == 1 {
79047785
g.sb.write_string('((void (*)(int))')
7905-
g.gen_expr(call_args[arg_idx])
7786+
g.gen_expr(call_args[i])
79067787
g.sb.write_string(')')
7907-
arg_idx++
79087788
continue
79097789
}
7910-
g.gen_call_arg(c_name, i, call_args[arg_idx])
7911-
arg_idx++
7790+
g.gen_call_arg(c_name, i, call_args[i])
79127791
} else {
79137792
// Default arguments should be lowered by transformer; keep C generation moving.
79147793
if param_types := g.fn_param_types[c_name] {

0 commit comments

Comments
 (0)