1use std::collections::HashSet;
5
6use super::Lowerer;
7
8impl Lowerer {
9 pub(crate) fn collect_returned_var_zig_names_in_block(
13 block: &crate::zigir::types::IrBlock,
14 ) -> HashSet<String> {
15 let mut names = HashSet::new();
16 for stmt in &block.stmts {
17 Self::collect_returned_var_zig_names_in_stmt(stmt, &mut names);
18 }
19 names
20 }
21
22 pub(crate) fn clear_deinit_for_returned_vars(body: &mut crate::zigir::types::IrBlock) {
25 let returned_vars = Self::collect_returned_var_zig_names_in_block(body);
26 if !returned_vars.is_empty() {
27 for stmt in &mut body.stmts {
28 if let crate::zigir::types::IrStmt::VarDecl(vd) = stmt
29 && vd.needs_deinit
30 && returned_vars.contains(&vd.name.zig_name)
31 {
32 vd.needs_deinit = false;
33 }
34 }
35 }
36 }
37
38 fn collect_returned_var_zig_names_in_stmt(
39 stmt: &crate::zigir::types::IrStmt,
40 names: &mut HashSet<String>,
41 ) {
42 use crate::zigir::types::IrStmt;
43 match stmt {
44 IrStmt::Return { value: Some(expr) } => {
45 Self::collect_returned_idents_in_expr(expr, names);
46 }
47 IrStmt::If { then, else_, .. } => {
48 for s in &then.stmts {
49 Self::collect_returned_var_zig_names_in_stmt(s, names);
50 }
51 if let Some(e) = else_ {
52 for s in &e.stmts {
53 Self::collect_returned_var_zig_names_in_stmt(s, names);
54 }
55 }
56 }
57 IrStmt::Block(b) => {
58 for s in &b.stmts {
59 Self::collect_returned_var_zig_names_in_stmt(s, names);
60 }
61 }
62 IrStmt::Try {
63 try_block,
64 catch_block,
65 finally,
66 ..
67 } => {
68 for s in &try_block.stmts {
69 Self::collect_returned_var_zig_names_in_stmt(s, names);
70 }
71 for s in &catch_block.stmts {
72 Self::collect_returned_var_zig_names_in_stmt(s, names);
73 }
74 if let Some(f) = finally {
75 for s in &f.stmts {
76 Self::collect_returned_var_zig_names_in_stmt(s, names);
77 }
78 }
79 }
80 IrStmt::While { body, .. } | IrStmt::DoWhile { body, .. } => {
81 for s in &body.stmts {
82 Self::collect_returned_var_zig_names_in_stmt(s, names);
83 }
84 }
85 IrStmt::For { body, .. } => {
86 for s in &body.stmts {
87 Self::collect_returned_var_zig_names_in_stmt(s, names);
88 }
89 }
90 IrStmt::ForIn { body, .. } | IrStmt::ForOf { body, .. } => {
91 for s in &body.stmts {
92 Self::collect_returned_var_zig_names_in_stmt(s, names);
93 }
94 }
95 IrStmt::Switch { cases, .. } => {
96 for c in cases {
97 for s in &c.body {
98 Self::collect_returned_var_zig_names_in_stmt(s, names);
99 }
100 }
101 }
102 _ => {}
105 }
106 }
107
108 fn collect_returned_idents_in_expr(
114 expr: &crate::zigir::types::IrExpr,
115 names: &mut HashSet<String>,
116 ) {
117 use crate::zigir::types::{IrExpr, IrObjectItem};
118 match expr {
119 IrExpr::Ident(ident) | IrExpr::TypedIdent { ident, .. } => {
120 names.insert(ident.zig_name.clone());
121 }
122 IrExpr::ObjectLiteral(ol) => {
123 for item in &ol.items {
124 match item {
125 IrObjectItem::Field(f) => {
126 Self::collect_returned_idents_in_expr(&f.value, names);
127 }
128 IrObjectItem::Spread(e) => {
129 Self::collect_returned_idents_in_expr(e, names);
130 }
131 }
132 }
133 }
134 IrExpr::ArrayLiteral(al) => {
135 for e in &al.elements {
136 Self::collect_returned_idents_in_expr(e, names);
137 }
138 }
139 IrExpr::Paren(inner) => {
140 Self::collect_returned_idents_in_expr(inner, names);
141 }
142 IrExpr::Conditional { then, else_, .. } => {
143 Self::collect_returned_idents_in_expr(then, names);
144 Self::collect_returned_idents_in_expr(else_, names);
145 }
146 IrExpr::Sequence(exprs) => {
147 for e in exprs {
148 Self::collect_returned_idents_in_expr(e, names);
149 }
150 }
151 _ => {}
155 }
156 }
157
158 pub(crate) fn collect_ir_idents_in_block(
161 block: &crate::zigir::types::IrBlock,
162 ) -> HashSet<String> {
163 let mut idents = HashSet::new();
164 for stmt in &block.stmts {
165 Self::collect_ir_idents_in_stmt(stmt, &mut idents);
166 }
167 idents
168 }
169
170 pub(super) fn collect_ir_idents_in_stmt(
171 stmt: &crate::zigir::types::IrStmt,
172 idents: &mut HashSet<String>,
173 ) {
174 use crate::zigir::types::IrStmt;
175 match stmt {
176 IrStmt::VarDecl(vd) => {
177 if let Some(init) = &vd.init {
178 Self::collect_ir_idents_in_expr(init, idents);
179 }
180 }
181 IrStmt::Assign { target, value, .. } => {
182 Self::collect_ir_idents_in_assign_target(target, idents);
183 Self::collect_ir_idents_in_expr(value, idents);
184 }
185 IrStmt::If { cond, then, else_ } => {
186 Self::collect_ir_idents_in_expr(cond, idents);
187 for s in &then.stmts {
188 Self::collect_ir_idents_in_stmt(s, idents);
189 }
190 if let Some(e) = else_ {
191 for s in &e.stmts {
192 Self::collect_ir_idents_in_stmt(s, idents);
193 }
194 }
195 }
196 IrStmt::While { cond, body, .. } | IrStmt::DoWhile { cond, body, .. } => {
197 Self::collect_ir_idents_in_expr(cond, idents);
198 for s in &body.stmts {
199 Self::collect_ir_idents_in_stmt(s, idents);
200 }
201 }
202 IrStmt::For {
203 init,
204 cond,
205 update,
206 body,
207 ..
208 } => {
209 if let Some(s) = init {
210 Self::collect_ir_idents_in_stmt(s, idents);
211 }
212 if let Some(e) = cond {
213 Self::collect_ir_idents_in_expr(e, idents);
214 }
215 if let Some(s) = update {
216 Self::collect_ir_idents_in_stmt(s, idents);
217 }
218 for s in &body.stmts {
219 Self::collect_ir_idents_in_stmt(s, idents);
220 }
221 }
222 IrStmt::ForIn { iterable, body, .. } | IrStmt::ForOf { iterable, body, .. } => {
223 Self::collect_ir_idents_in_expr(iterable, idents);
224 for s in &body.stmts {
225 Self::collect_ir_idents_in_stmt(s, idents);
226 }
227 }
228 IrStmt::Switch { expr, cases } => {
229 Self::collect_ir_idents_in_expr(expr, idents);
230 for c in cases {
231 if let Some(e) = &c.test {
232 Self::collect_ir_idents_in_expr(e, idents);
233 }
234 for s in &c.body {
235 Self::collect_ir_idents_in_stmt(s, idents);
236 }
237 }
238 }
239 IrStmt::Try {
240 try_block,
241 catch_block,
242 finally,
243 ..
244 } => {
245 for s in &try_block.stmts {
246 Self::collect_ir_idents_in_stmt(s, idents);
247 }
248 for s in &catch_block.stmts {
249 Self::collect_ir_idents_in_stmt(s, idents);
250 }
251 if let Some(f) = finally {
252 for s in &f.stmts {
253 Self::collect_ir_idents_in_stmt(s, idents);
254 }
255 }
256 }
257 IrStmt::Throw { value, .. } => {
258 Self::collect_ir_idents_in_expr(value, idents);
259 }
260 IrStmt::Return { value } => {
261 if let Some(e) = value {
262 Self::collect_ir_idents_in_expr(e, idents);
263 }
264 }
265 IrStmt::Break { .. } | IrStmt::Continue { .. } => {}
266 IrStmt::Expr(e) => {
267 Self::collect_ir_idents_in_expr(e, idents);
268 }
269 IrStmt::Block(b) => {
270 for s in &b.stmts {
271 Self::collect_ir_idents_in_stmt(s, idents);
272 }
273 }
274 IrStmt::CompileError { .. } | IrStmt::Comment(_) => {}
275 IrStmt::DestructureDecl(data) => {
276 Self::collect_ir_idents_in_expr(&data.init, idents);
277 for binding in &data.bindings {
278 if let Some(d) = &binding.default {
279 Self::collect_ir_idents_in_expr(d, idents);
280 }
281 }
282 }
283 IrStmt::NestedFnDecl {
284 struct_def,
285 instance,
286 } => {
287 for s in &struct_def.body.stmts {
288 Self::collect_ir_idents_in_stmt(s, idents);
289 }
290 if let Some(closure) = instance {
291 for cap in &closure.captured {
292 idents.insert(cap.name.js_name.clone());
293 }
294 }
295 }
296 }
297 }
298
299 pub(super) fn collect_ir_idents_in_assign_target(
300 target: &crate::zigir::types::IrAssignTarget,
301 idents: &mut HashSet<String>,
302 ) {
303 use crate::zigir::types::IrAssignTarget;
304 match target {
305 IrAssignTarget::Ident(name) => {
306 idents.insert(name.js_name.clone());
307 }
308 IrAssignTarget::Member { object, .. } => {
309 Self::collect_ir_idents_in_expr(object, idents);
310 }
311 IrAssignTarget::Index { object, index, .. } => {
312 Self::collect_ir_idents_in_expr(object, idents);
313 Self::collect_ir_idents_in_expr(index, idents);
314 }
315 IrAssignTarget::Destructure(bindings) => {
316 for b in bindings {
317 if let Some(d) = &b.default {
318 Self::collect_ir_idents_in_expr(d, idents);
319 }
320 }
321 }
322 IrAssignTarget::CompileError { .. } => {}
323 }
324 }
325
326 pub(crate) fn collect_ast_expr_idents(
329 expr: &oxc_ast::ast::Expression,
330 idents: &mut HashSet<String>,
331 ) {
332 use oxc_ast::ast::Expression;
333 match expr {
334 Expression::Identifier(id) => {
335 idents.insert(id.name.to_string());
336 }
337 Expression::BinaryExpression(be) => {
338 Self::collect_ast_expr_idents(&be.left, idents);
339 Self::collect_ast_expr_idents(&be.right, idents);
340 }
341 Expression::UnaryExpression(ue) => {
342 Self::collect_ast_expr_idents(&ue.argument, idents);
343 }
344 Expression::CallExpression(ce) => {
345 Self::collect_ast_expr_idents(&ce.callee, idents);
346 }
347 Expression::StaticMemberExpression(me) => {
348 Self::collect_ast_expr_idents(&me.object, idents);
349 }
350 Expression::ComputedMemberExpression(me) => {
351 Self::collect_ast_expr_idents(&me.object, idents);
352 }
353 Expression::ParenthesizedExpression(pe) => {
354 Self::collect_ast_expr_idents(&pe.expression, idents);
355 }
356 _ => {}
357 }
358 }
359
360 pub(super) fn collect_ir_idents_in_expr(
361 expr: &crate::zigir::types::IrExpr,
362 idents: &mut HashSet<String>,
363 ) {
364 use crate::zigir::types::IrExpr;
365 match expr {
366 IrExpr::Ident(name) | IrExpr::TypedIdent { ident: name, .. } => {
367 idents.insert(name.js_name.clone());
368 }
369 IrExpr::Binary { left, right, .. } | IrExpr::Logical { left, right, .. } => {
370 Self::collect_ir_idents_in_expr(left, idents);
371 Self::collect_ir_idents_in_expr(right, idents);
372 }
373 IrExpr::Unary { operand, .. }
374 | IrExpr::Typeof(operand)
375 | IrExpr::Void(operand)
376 | IrExpr::Paren(operand)
377 | IrExpr::Spread(operand) => {
378 Self::collect_ir_idents_in_expr(operand, idents);
379 }
380 IrExpr::Update { target, .. } => {
381 Self::collect_ir_idents_in_assign_target(target, idents);
382 }
383 IrExpr::Assign { target, value, .. } => {
384 Self::collect_ir_idents_in_assign_target(target, idents);
385 Self::collect_ir_idents_in_expr(value, idents);
386 }
387 IrExpr::Call(call) => {
388 Self::collect_ir_idents_in_expr(&call.callee, idents);
389 for a in &call.args {
390 Self::collect_ir_idents_in_expr(a, idents);
391 }
392 }
393 IrExpr::BuiltinCall(bc) => {
394 if let Some(ref obj) = bc.obj_name {
395 idents.insert(obj.clone());
396 }
397 if let Some(ref obj_expr) = bc.obj_expr {
398 Self::collect_ir_idents_in_expr(obj_expr, idents);
399 }
400 for a in &bc.args {
401 Self::collect_ir_idents_in_expr(a, idents);
402 }
403 }
404 IrExpr::HostCall(hc) => {
405 for a in &hc.args {
406 Self::collect_ir_idents_in_expr(a, idents);
407 }
408 }
409 IrExpr::FieldAccess { object, .. }
410 | IrExpr::IndexAccess { object, .. }
411 | IrExpr::ComputedField { object, .. } => {
412 Self::collect_ir_idents_in_expr(object, idents);
413 if let IrExpr::IndexAccess { index, .. } = expr {
414 Self::collect_ir_idents_in_expr(index, idents);
415 }
416 if let IrExpr::ComputedField { key, .. } = expr {
417 Self::collect_ir_idents_in_expr(key, idents);
418 }
419 }
420 IrExpr::Conditional { cond, then, else_ } => {
421 Self::collect_ir_idents_in_expr(cond, idents);
422 Self::collect_ir_idents_in_expr(then, idents);
423 Self::collect_ir_idents_in_expr(else_, idents);
424 }
425 IrExpr::Closure(c) => {
426 for cap in &c.captured {
427 idents.insert(cap.name.js_name.clone());
428 }
429 for s in &c.body.stmts {
430 Self::collect_ir_idents_in_stmt(s, idents);
431 }
432 }
433 IrExpr::ArrowFn(a) => {
434 for s in &a.body.stmts {
435 Self::collect_ir_idents_in_stmt(s, idents);
436 }
437 }
438 IrExpr::FnExpr(f) => {
439 for s in &f.body.stmts {
440 Self::collect_ir_idents_in_stmt(s, idents);
441 }
442 }
443 IrExpr::ArrayLiteral(al) => {
444 for e in &al.elements {
445 Self::collect_ir_idents_in_expr(e, idents);
446 }
447 }
448 IrExpr::ObjectLiteral(ol) => {
449 use crate::zigir::types::IrObjectItem;
450 for item in &ol.items {
451 match item {
452 IrObjectItem::Field(f) => {
453 Self::collect_ir_idents_in_expr(&f.value, idents);
454 }
455 IrObjectItem::Spread(e) => {
456 Self::collect_ir_idents_in_expr(e, idents);
457 }
458 }
459 }
460 }
461 IrExpr::New(ne) => {
462 for a in &ne.args {
463 Self::collect_ir_idents_in_expr(a, idents);
464 }
465 }
466 IrExpr::TemplateLiteral { exprs, .. } => {
467 for e in exprs {
468 Self::collect_ir_idents_in_expr(e, idents);
469 }
470 }
471 IrExpr::AllocPrint { args, .. } => {
472 for a in args {
473 Self::collect_ir_idents_in_expr(a, idents);
474 }
475 }
476 IrExpr::BlockExpr { body, result, .. } => {
477 for s in body {
478 Self::collect_ir_idents_in_stmt(s, idents);
479 }
480 Self::collect_ir_idents_in_expr(result, idents);
481 }
482 IrExpr::Sequence(exprs) => {
483 for e in exprs {
484 Self::collect_ir_idents_in_expr(e, idents);
485 }
486 }
487 IrExpr::Await(ae) => {
488 Self::collect_ir_idents_in_expr(&ae.callee, idents);
489 for a in &ae.args {
490 Self::collect_ir_idents_in_expr(a, idents);
491 }
492 }
493 IrExpr::ArrayCallbackInline(inline_data) => {
494 for s in &inline_data.body {
495 Self::collect_ir_idents_in_stmt(s, idents);
496 }
497 if let Some(ref init) = inline_data.reduce_init {
498 Self::collect_ir_idents_in_expr(init, idents);
499 }
500 }
501 IrExpr::ArrayMethodInline(inline_data) => {
502 for arg in &inline_data.args {
503 Self::collect_ir_idents_in_expr(arg, idents);
504 }
505 }
506 IrExpr::OptionalChain { object, body, .. } => {
507 Self::collect_ir_idents_in_expr(object, idents);
508 Self::collect_ir_idents_in_expr(body, idents);
509 }
510 IrExpr::PowExpr { base, exp, .. } => {
511 Self::collect_ir_idents_in_expr(base, idents);
512 Self::collect_ir_idents_in_expr(exp, idents);
513 }
514 IrExpr::RemExpr { left, right, .. } => {
515 Self::collect_ir_idents_in_expr(left, idents);
516 Self::collect_ir_idents_in_expr(right, idents);
517 }
518 IrExpr::DivExpr { left, right, .. } => {
519 Self::collect_ir_idents_in_expr(left, idents);
520 Self::collect_ir_idents_in_expr(right, idents);
521 }
522 IrExpr::IntLiteral(_)
523 | IrExpr::FloatLiteral(_)
524 | IrExpr::StringLiteral(_)
525 | IrExpr::BoolLiteral(_)
526 | IrExpr::BigIntLiteral(_)
527 | IrExpr::Null
528 | IrExpr::Undefined
529 | IrExpr::This
530 | IrExpr::CompileError { .. } => {}
531 }
532 }
533}
534
535#[cfg(test)]
536mod tests {
537 use super::*;
538 use crate::types::ZigType;
539 use crate::zigir::ident::IrIdent;
540 use crate::zigir::types::{IrBlock, IrExpr, IrStmt, IrVarDecl};
541
542 #[test]
543 fn test_collect_returned_var_direct() {
544 let block = IrBlock::new(vec![
546 IrStmt::VarDecl(IrVarDecl {
547 name: IrIdent::new("myMap"),
548 is_const: true,
549 zig_type: Some(ZigType::NamedStruct("Map".to_string())),
550 init: None,
551 is_json_parse: false,
552 needs_var_suppression: false,
553 needs_deinit: true,
554 }),
555 IrStmt::Return {
556 value: Some(IrExpr::Ident(IrIdent::new("myMap"))),
557 },
558 ]);
559 let names = Lowerer::collect_returned_var_zig_names_in_block(&block);
560 assert!(names.contains("myMap"));
561 assert_eq!(names.len(), 1);
562 }
563
564 #[test]
565 fn test_collect_returned_var_in_if() {
566 let block = IrBlock::new(vec![IrStmt::If {
568 cond: IrExpr::BoolLiteral(true),
569 then: IrBlock::new(vec![IrStmt::Return {
570 value: Some(IrExpr::Ident(IrIdent::new("s"))),
571 }]),
572 else_: None,
573 }]);
574 let names = Lowerer::collect_returned_var_zig_names_in_block(&block);
575 assert!(names.contains("s"));
576 }
577
578 #[test]
579 fn test_collect_returned_var_not_ident() {
580 let block = IrBlock::new(vec![IrStmt::Return {
582 value: Some(IrExpr::IntLiteral(42)),
583 }]);
584 let names = Lowerer::collect_returned_var_zig_names_in_block(&block);
585 assert!(names.is_empty());
586 }
587
588 #[test]
589 fn test_collect_returned_var_no_return() {
590 let block = IrBlock::new(vec![IrStmt::VarDecl(IrVarDecl {
592 name: IrIdent::new("m"),
593 is_const: true,
594 zig_type: Some(ZigType::NamedStruct("Map".to_string())),
595 init: None,
596 is_json_parse: false,
597 needs_var_suppression: false,
598 needs_deinit: true,
599 })]);
600 let names = Lowerer::collect_returned_var_zig_names_in_block(&block);
601 assert!(names.is_empty());
602 }
603
604 #[test]
605 fn test_collect_returned_var_nested_object() {
606 use crate::zigir::types::{IrObjectField, IrObjectItem, IrObjectLiteral};
608 let block = IrBlock::new(vec![
609 IrStmt::VarDecl(IrVarDecl {
610 name: IrIdent::new("m"),
611 is_const: true,
612 zig_type: Some(ZigType::NamedStruct("Map".to_string())),
613 init: None,
614 is_json_parse: false,
615 needs_var_suppression: false,
616 needs_deinit: true,
617 }),
618 IrStmt::Return {
619 value: Some(IrExpr::ObjectLiteral(IrObjectLiteral {
620 items: vec![IrObjectItem::Field(IrObjectField {
621 key: "map".to_string(),
622 value: IrExpr::Ident(IrIdent::new("m")),
623 is_computed: false,
624 })],
625 })),
626 },
627 ]);
628 let names = Lowerer::collect_returned_var_zig_names_in_block(&block);
629 assert!(
630 names.contains("m"),
631 "nested object field ident should be collected"
632 );
633 }
634
635 #[test]
636 fn test_collect_returned_var_nested_array() {
637 let block = IrBlock::new(vec![IrStmt::Return {
639 value: Some(IrExpr::ArrayLiteral(crate::zigir::types::IrArrayLiteral {
640 elements: vec![IrExpr::Ident(IrIdent::new("m"))],
641 spread_indices: vec![],
642 })),
643 }]);
644 let names = Lowerer::collect_returned_var_zig_names_in_block(&block);
645 assert!(
646 names.contains("m"),
647 "nested array element ident should be collected"
648 );
649 }
650
651 #[test]
652 fn test_collect_returned_var_paren() {
653 let block = IrBlock::new(vec![IrStmt::Return {
655 value: Some(IrExpr::Paren(Box::new(IrExpr::Ident(IrIdent::new("m"))))),
656 }]);
657 let names = Lowerer::collect_returned_var_zig_names_in_block(&block);
658 assert!(names.contains("m"));
659 }
660
661 #[test]
662 fn test_collect_returned_var_conditional() {
663 let block = IrBlock::new(vec![IrStmt::Return {
665 value: Some(IrExpr::Conditional {
666 cond: Box::new(IrExpr::BoolLiteral(true)),
667 then: Box::new(IrExpr::Ident(IrIdent::new("a"))),
668 else_: Box::new(IrExpr::Ident(IrIdent::new("b"))),
669 }),
670 }]);
671 let names = Lowerer::collect_returned_var_zig_names_in_block(&block);
672 assert!(names.contains("a"));
673 assert!(names.contains("b"));
674 }
675
676 #[test]
677 fn test_collect_returned_var_not_method_call() {
678 use crate::zigir::ops::BinOp;
680 let block = IrBlock::new(vec![IrStmt::Return {
681 value: Some(IrExpr::Binary {
682 op: BinOp::Add,
683 left: Box::new(IrExpr::Ident(IrIdent::new("m"))),
684 right: Box::new(IrExpr::IntLiteral(1)),
685 left_type: None,
686 right_type: None,
687 }),
688 }]);
689 let names = Lowerer::collect_returned_var_zig_names_in_block(&block);
690 assert!(
691 !names.contains("m"),
692 "binary expression should not transfer ownership"
693 );
694 }
695}