|
2 | 2 | //! |
3 | 3 | //! 这些 pass 经常需要回答同一类问题: |
4 | 4 | //! - 某个 binding 在一段语句里还会不会再被读取? |
| 5 | +//! - 某个语句实际提到了哪些 binding(包括赋值目标这种 mention,而不只是读取)? |
5 | 6 | //! - 某个语句/块会不会提前引用一组待下沉的 hoisted local? |
6 | 7 | //! - 某个 binding 在当前函数体里一共被用了几次? |
7 | 8 | //! |
|
11 | 12 | //! 但 `FunctionExpr.captured_bindings` 是闭包创建时对当前词法 binding 的显式引用, |
12 | 13 | //! 必须按当前语句的一次使用统计,否则后续 pass 可能误删仍被闭包持有的局部。 |
13 | 14 |
|
14 | | -use std::collections::BTreeMap; |
| 15 | +use std::collections::{BTreeMap, BTreeSet}; |
15 | 16 |
|
16 | 17 | use super::super::common::{ |
17 | 18 | AstBindingRef, AstBlock, AstCallKind, AstExpr, AstLValue, AstLocalBinding, AstNameRef, AstStmt, |
@@ -173,6 +174,12 @@ pub(super) fn count_binding_mentions_in_block(block: &AstBlock, binding: AstBind |
173 | 174 | .sum() |
174 | 175 | } |
175 | 176 |
|
| 177 | +pub(super) fn binding_mentions_in_stmt(stmt: &AstStmt) -> BTreeSet<AstBindingRef> { |
| 178 | + let mut mentions = BTreeSet::new(); |
| 179 | + collect_binding_mentions_in_stmt(stmt, &mut mentions); |
| 180 | + mentions |
| 181 | +} |
| 182 | + |
176 | 183 | pub(super) fn count_binding_uses_in_stmt(stmt: &AstStmt, binding: AstBindingRef) -> usize { |
177 | 184 | count_binding_uses_in_stmt_with_scope(stmt, binding, BindingUseScope::CurrentFunctionOnly) |
178 | 185 | } |
@@ -451,6 +458,193 @@ fn count_binding_mentions_in_stmt(stmt: &AstStmt, binding: AstBindingRef) -> usi |
451 | 458 | } |
452 | 459 | } |
453 | 460 |
|
| 461 | +fn collect_binding_mentions_in_block(block: &AstBlock, mentions: &mut BTreeSet<AstBindingRef>) { |
| 462 | + for stmt in &block.stmts { |
| 463 | + collect_binding_mentions_in_stmt(stmt, mentions); |
| 464 | + } |
| 465 | +} |
| 466 | + |
| 467 | +fn collect_binding_mentions_in_stmt(stmt: &AstStmt, mentions: &mut BTreeSet<AstBindingRef>) { |
| 468 | + match stmt { |
| 469 | + AstStmt::LocalDecl(local_decl) => { |
| 470 | + mentions.extend(local_decl.bindings.iter().map(|binding| binding.id)); |
| 471 | + for value in &local_decl.values { |
| 472 | + collect_binding_mentions_in_expr(value, mentions); |
| 473 | + } |
| 474 | + } |
| 475 | + AstStmt::GlobalDecl(global_decl) => { |
| 476 | + for value in &global_decl.values { |
| 477 | + collect_binding_mentions_in_expr(value, mentions); |
| 478 | + } |
| 479 | + } |
| 480 | + AstStmt::Assign(assign) => { |
| 481 | + for target in &assign.targets { |
| 482 | + collect_binding_mentions_in_lvalue(target, mentions); |
| 483 | + } |
| 484 | + for value in &assign.values { |
| 485 | + collect_binding_mentions_in_expr(value, mentions); |
| 486 | + } |
| 487 | + } |
| 488 | + AstStmt::CallStmt(call_stmt) => collect_binding_mentions_in_call(&call_stmt.call, mentions), |
| 489 | + AstStmt::Return(ret) => { |
| 490 | + for value in &ret.values { |
| 491 | + collect_binding_mentions_in_expr(value, mentions); |
| 492 | + } |
| 493 | + } |
| 494 | + AstStmt::If(if_stmt) => { |
| 495 | + collect_binding_mentions_in_expr(&if_stmt.cond, mentions); |
| 496 | + collect_binding_mentions_in_block(&if_stmt.then_block, mentions); |
| 497 | + if let Some(else_block) = &if_stmt.else_block { |
| 498 | + collect_binding_mentions_in_block(else_block, mentions); |
| 499 | + } |
| 500 | + } |
| 501 | + AstStmt::While(while_stmt) => { |
| 502 | + collect_binding_mentions_in_expr(&while_stmt.cond, mentions); |
| 503 | + collect_binding_mentions_in_block(&while_stmt.body, mentions); |
| 504 | + } |
| 505 | + AstStmt::Repeat(repeat_stmt) => { |
| 506 | + collect_binding_mentions_in_block(&repeat_stmt.body, mentions); |
| 507 | + collect_binding_mentions_in_expr(&repeat_stmt.cond, mentions); |
| 508 | + } |
| 509 | + AstStmt::NumericFor(numeric_for) => { |
| 510 | + mentions.insert(numeric_for.binding); |
| 511 | + collect_binding_mentions_in_expr(&numeric_for.start, mentions); |
| 512 | + collect_binding_mentions_in_expr(&numeric_for.limit, mentions); |
| 513 | + collect_binding_mentions_in_expr(&numeric_for.step, mentions); |
| 514 | + collect_binding_mentions_in_block(&numeric_for.body, mentions); |
| 515 | + } |
| 516 | + AstStmt::GenericFor(generic_for) => { |
| 517 | + mentions.extend(generic_for.bindings.iter().copied()); |
| 518 | + for expr in &generic_for.iterator { |
| 519 | + collect_binding_mentions_in_expr(expr, mentions); |
| 520 | + } |
| 521 | + collect_binding_mentions_in_block(&generic_for.body, mentions); |
| 522 | + } |
| 523 | + AstStmt::DoBlock(block) => collect_binding_mentions_in_block(block, mentions), |
| 524 | + AstStmt::FunctionDecl(function_decl) => { |
| 525 | + collect_function_name_mentions(&function_decl.target, mentions); |
| 526 | + } |
| 527 | + AstStmt::LocalFunctionDecl(function_decl) => { |
| 528 | + mentions.insert(function_decl.name); |
| 529 | + } |
| 530 | + AstStmt::Break |
| 531 | + | AstStmt::Continue |
| 532 | + | AstStmt::Goto(_) |
| 533 | + | AstStmt::Label(_) |
| 534 | + | AstStmt::Error(_) => {} |
| 535 | + } |
| 536 | +} |
| 537 | + |
| 538 | +fn collect_binding_mentions_in_call(call: &AstCallKind, mentions: &mut BTreeSet<AstBindingRef>) { |
| 539 | + match call { |
| 540 | + AstCallKind::Call(call) => { |
| 541 | + collect_binding_mentions_in_expr(&call.callee, mentions); |
| 542 | + for arg in &call.args { |
| 543 | + collect_binding_mentions_in_expr(arg, mentions); |
| 544 | + } |
| 545 | + } |
| 546 | + AstCallKind::MethodCall(call) => { |
| 547 | + collect_binding_mentions_in_expr(&call.receiver, mentions); |
| 548 | + for arg in &call.args { |
| 549 | + collect_binding_mentions_in_expr(arg, mentions); |
| 550 | + } |
| 551 | + } |
| 552 | + } |
| 553 | +} |
| 554 | + |
| 555 | +fn collect_binding_mentions_in_lvalue(target: &AstLValue, mentions: &mut BTreeSet<AstBindingRef>) { |
| 556 | + match target { |
| 557 | + AstLValue::Name(name) => { |
| 558 | + if let Some(binding) = binding_from_name_ref(name) { |
| 559 | + mentions.insert(binding); |
| 560 | + } |
| 561 | + } |
| 562 | + AstLValue::FieldAccess(access) => { |
| 563 | + collect_binding_mentions_in_expr(&access.base, mentions); |
| 564 | + } |
| 565 | + AstLValue::IndexAccess(access) => { |
| 566 | + collect_binding_mentions_in_expr(&access.base, mentions); |
| 567 | + collect_binding_mentions_in_expr(&access.index, mentions); |
| 568 | + } |
| 569 | + } |
| 570 | +} |
| 571 | + |
| 572 | +fn collect_binding_mentions_in_expr(expr: &AstExpr, mentions: &mut BTreeSet<AstBindingRef>) { |
| 573 | + match expr { |
| 574 | + AstExpr::Var(name) => { |
| 575 | + if let Some(binding) = binding_from_name_ref(name) { |
| 576 | + mentions.insert(binding); |
| 577 | + } |
| 578 | + } |
| 579 | + AstExpr::FieldAccess(access) => collect_binding_mentions_in_expr(&access.base, mentions), |
| 580 | + AstExpr::IndexAccess(access) => { |
| 581 | + collect_binding_mentions_in_expr(&access.base, mentions); |
| 582 | + collect_binding_mentions_in_expr(&access.index, mentions); |
| 583 | + } |
| 584 | + AstExpr::Unary(unary) => collect_binding_mentions_in_expr(&unary.expr, mentions), |
| 585 | + AstExpr::Binary(binary) => { |
| 586 | + collect_binding_mentions_in_expr(&binary.lhs, mentions); |
| 587 | + collect_binding_mentions_in_expr(&binary.rhs, mentions); |
| 588 | + } |
| 589 | + AstExpr::LogicalAnd(logical) | AstExpr::LogicalOr(logical) => { |
| 590 | + collect_binding_mentions_in_expr(&logical.lhs, mentions); |
| 591 | + collect_binding_mentions_in_expr(&logical.rhs, mentions); |
| 592 | + } |
| 593 | + AstExpr::Call(call) => { |
| 594 | + collect_binding_mentions_in_expr(&call.callee, mentions); |
| 595 | + for arg in &call.args { |
| 596 | + collect_binding_mentions_in_expr(arg, mentions); |
| 597 | + } |
| 598 | + } |
| 599 | + AstExpr::MethodCall(call) => { |
| 600 | + collect_binding_mentions_in_expr(&call.receiver, mentions); |
| 601 | + for arg in &call.args { |
| 602 | + collect_binding_mentions_in_expr(arg, mentions); |
| 603 | + } |
| 604 | + } |
| 605 | + AstExpr::SingleValue(expr) => collect_binding_mentions_in_expr(expr, mentions), |
| 606 | + AstExpr::TableConstructor(table) => { |
| 607 | + for field in &table.fields { |
| 608 | + match field { |
| 609 | + AstTableField::Array(value) => { |
| 610 | + collect_binding_mentions_in_expr(value, mentions); |
| 611 | + } |
| 612 | + AstTableField::Record(record) => { |
| 613 | + if let AstTableKey::Expr(key) = &record.key { |
| 614 | + collect_binding_mentions_in_expr(key, mentions); |
| 615 | + } |
| 616 | + collect_binding_mentions_in_expr(&record.value, mentions); |
| 617 | + } |
| 618 | + } |
| 619 | + } |
| 620 | + } |
| 621 | + AstExpr::FunctionExpr(_) => {} |
| 622 | + AstExpr::Nil |
| 623 | + | AstExpr::Boolean(_) |
| 624 | + | AstExpr::Integer(_) |
| 625 | + | AstExpr::Number(_) |
| 626 | + | AstExpr::String(_) |
| 627 | + | AstExpr::Int64(_) |
| 628 | + | AstExpr::UInt64(_) |
| 629 | + | AstExpr::Complex { .. } |
| 630 | + | AstExpr::VarArg |
| 631 | + | AstExpr::Error(_) => {} |
| 632 | + } |
| 633 | +} |
| 634 | + |
| 635 | +fn collect_function_name_mentions( |
| 636 | + target: &super::super::common::AstFunctionName, |
| 637 | + mentions: &mut BTreeSet<AstBindingRef>, |
| 638 | +) { |
| 639 | + let path = match target { |
| 640 | + super::super::common::AstFunctionName::Plain(path) => path, |
| 641 | + super::super::common::AstFunctionName::Method(path, _) => path, |
| 642 | + }; |
| 643 | + if let Some(binding) = binding_from_name_ref(&path.root) { |
| 644 | + mentions.insert(binding); |
| 645 | + } |
| 646 | +} |
| 647 | + |
454 | 648 | fn count_binding_uses_in_call(call: &AstCallKind, binding: AstBindingRef) -> usize { |
455 | 649 | count_binding_uses_in_call_with_scope(call, binding, BindingUseScope::CurrentFunctionOnly) |
456 | 650 | } |
|
0 commit comments