Skip to content

Commit e9376a8

Browse files
committed
transformer: IndexExpr
1 parent bcfc069 commit e9376a8

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

vlib/v/transformer/array.v

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import v.ast
88
pub fn (mut t Transformer) array_init(mut node ast.ArrayInit) ast.Expr {
99
// For JS and Go generate array init using their syntax
1010
// if t.pref.backend !in [.c, .native] {
11-
if !t.pref.new_transform || node.is_fixed {
11+
if !t.pref.new_transform || node.is_fixed { // || t.file.mod.name != 'main' {
1212
for mut expr in node.exprs {
1313
expr = t.expr(mut expr)
1414
}
@@ -127,6 +127,72 @@ pub fn (mut t Transformer) array_init(mut node ast.ArrayInit) ast.Expr {
127127
//}
128128
}
129129

130+
pub fn (mut t Transformer) index_expr(mut node ast.IndexExpr) ast.Expr {
131+
// if t.pref.new_transform {
132+
// println(t.file.mod)
133+
//}
134+
t.check_safe_array(mut node)
135+
node.left = t.expr(mut node.left)
136+
node.index = t.expr(mut node.index)
137+
node.or_expr = t.expr(mut node.or_expr) as ast.OrExpr
138+
// if true { //!t.pref.new_transform { //|| t.file.mod.name != 'main' {
139+
if !t.pref.new_transform { //|| t.file.mod.name != 'main' {
140+
return node
141+
}
142+
// Recursively transform the left side (array/map) and the index
143+
node.left = t.expr(mut node.left)
144+
node.index = t.expr(mut node.index)
145+
// Only apply transformation if it is an array (not a fixed array, map, or option)
146+
if node.is_array && !node.is_farray && !node.is_option && !node.is_map {
147+
// Transformation: x[i] => (*(T*)builtin__array_get(x, i))
148+
mut left_expr := node.left
149+
mut left_type := node.left_type
150+
// If the array is a pointer/reference (e.g. &[]int), we must dereference it
151+
// because builtin__array_get expects the array struct by value.
152+
if left_type.is_ptr() {
153+
left_expr = ast.PrefixExpr{
154+
op: .mul
155+
right: left_expr
156+
pos: node.pos
157+
}
158+
// The type of the argument becomes the value type (remove pointer)
159+
left_type = left_type.set_nr_muls(0)
160+
}
161+
// Create the call: builtin__array_get(x, i)
162+
call_expr := ast.CallExpr{
163+
name: 'builtin__array_get'
164+
mod: 'builtin'
165+
args: [
166+
ast.CallArg{
167+
expr: left_expr
168+
typ: left_type
169+
},
170+
ast.CallArg{
171+
expr: node.index
172+
typ: ast.int_type
173+
},
174+
]
175+
return_type: ast.voidptr_type
176+
}
177+
// Create the pointer type T* to cast the result (voidptr) to.
178+
// node.typ is the element type T.
179+
ptr_typ := node.typ.ref()
180+
// Create the cast: (T*)builtin__array_get(...)
181+
cast_expr := ast.CastExpr{
182+
expr: call_expr
183+
typ: ptr_typ
184+
typname: t.table.get_type_name(ptr_typ)
185+
}
186+
// Dereference the result to get the element: *...
187+
return ast.PrefixExpr{
188+
op: .mul
189+
right: cast_expr
190+
pos: node.pos
191+
}
192+
}
193+
return node
194+
}
195+
130196
pub fn (mut t Transformer) find_new_array_len(node ast.AssignStmt) {
131197
if !t.pref.is_prod {
132198
return

vlib/v/transformer/transformer.v

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ pub fn (mut t Transformer) transform(mut ast_file ast.File) {
5757
}
5858
}
5959

60-
6160
pub fn (mut t Transformer) find_new_range(node ast.AssignStmt) {
6261
if !t.pref.is_prod {
6362
return
@@ -578,10 +577,7 @@ pub fn (mut t Transformer) expr(mut node ast.Expr) ast.Expr {
578577
node.expr = t.expr(mut node.expr)
579578
}
580579
ast.IndexExpr {
581-
t.check_safe_array(mut node)
582-
node.left = t.expr(mut node.left)
583-
node.index = t.expr(mut node.index)
584-
node.or_expr = t.expr(mut node.or_expr) as ast.OrExpr
580+
return t.index_expr(mut node)
585581
}
586582
ast.InfixExpr {
587583
return t.infix_expr(mut node)
@@ -1120,7 +1116,6 @@ pub fn (mut t Transformer) infix_expr(mut node ast.InfixExpr) ast.Expr {
11201116
}
11211117
}
11221118

1123-
11241119
pub fn (mut t Transformer) if_expr(mut node ast.IfExpr) ast.Expr {
11251120
for mut branch in node.branches {
11261121
branch.cond = t.expr(mut branch.cond)

0 commit comments

Comments
 (0)