unluac 1.1.1

Multi-dialect Lua decompiler written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! 这个子模块负责 `inline_exprs` pass 的 use-site 重写。
//!
//! 它依赖 `candidate` 已经给好的候选类型和策略,只在允许的位置替换引用,不会回头重判
//! 候选本身是否安全。
//! 例如:`local r0 = print; r0(1)` 选中后,会在这里把调用位点改成 `print(1)`。

use crate::readability::ReadabilityOptions;

use super::super::super::common::{
    AstCallExpr, AstCallKind, AstExpr, AstGlobalDecl, AstLValue, AstMethodCallExpr, AstStmt,
    AstTableField, AstTableKey,
};
use super::super::binding_flow::name_matches_binding;
use super::super::expr_analysis::{
    expr_complexity, is_access_base_inline_expr, is_direct_return_constructor_inline_expr,
    is_mechanical_run_inline_expr,
};
use super::candidate::{
    InlineCandidate, InlinePolicy, is_call_callee_inline_expr,
    is_extended_call_arg_local_alias_expr, is_extended_neutral_local_alias_expr,
    is_lookup_inline_expr, is_raw_global_alias_expr, is_recallable_inline_expr,
};

pub(super) fn rewrite_stmt_use_sites_with_policy(
    stmt: &mut AstStmt,
    candidate: InlineCandidate,
    replacement: &AstExpr,
    options: ReadabilityOptions,
    policy: InlinePolicy,
) -> bool {
    match stmt {
        AstStmt::LocalDecl(local_decl) => rewrite_expr_list_context(
            &mut local_decl.values,
            candidate,
            replacement,
            InlineSite::Neutral,
            options,
            policy,
        ),
        AstStmt::GlobalDecl(global_decl) => {
            rewrite_global_decl_use_sites(global_decl, candidate, replacement, options, policy)
        }
        AstStmt::Assign(assign) => {
            let mut changed = false;
            for target in &mut assign.targets {
                changed |=
                    rewrite_lvalue_use_sites(target, candidate, replacement, options, policy);
            }
            changed |= rewrite_expr_list_context(
                &mut assign.values,
                candidate,
                replacement,
                InlineSite::Neutral,
                options,
                policy,
            );
            changed
        }
        AstStmt::CallStmt(call_stmt) => {
            rewrite_call_use_sites(&mut call_stmt.call, candidate, replacement, options, policy)
        }
        AstStmt::Return(ret) => rewrite_expr_list_context(
            &mut ret.values,
            candidate,
            replacement,
            InlineSite::ReturnValue,
            options,
            policy,
        ),
        AstStmt::If(if_stmt) => rewrite_expr_use_sites(
            &mut if_stmt.cond,
            candidate,
            replacement,
            InlineSite::Neutral,
            options,
            policy,
        ),
        AstStmt::While(while_stmt) => rewrite_expr_use_sites(
            &mut while_stmt.cond,
            candidate,
            replacement,
            InlineSite::Neutral,
            options,
            policy,
        ),
        AstStmt::Repeat(repeat_stmt) => rewrite_expr_use_sites(
            &mut repeat_stmt.cond,
            candidate,
            replacement,
            InlineSite::Neutral,
            options,
            policy,
        ),
        AstStmt::NumericFor(numeric_for) => {
            let mut changed = rewrite_expr_use_sites(
                &mut numeric_for.start,
                candidate,
                replacement,
                InlineSite::Neutral,
                options,
                policy,
            );
            changed |= rewrite_expr_use_sites(
                &mut numeric_for.limit,
                candidate,
                replacement,
                InlineSite::Neutral,
                options,
                policy,
            );
            changed |= rewrite_expr_use_sites(
                &mut numeric_for.step,
                candidate,
                replacement,
                InlineSite::Neutral,
                options,
                policy,
            );
            changed
        }
        AstStmt::GenericFor(generic_for) => rewrite_expr_list_context(
            &mut generic_for.iterator,
            candidate,
            replacement,
            InlineSite::Neutral,
            options,
            policy,
        ),
        AstStmt::DoBlock(_)
        | AstStmt::FunctionDecl(_)
        | AstStmt::LocalFunctionDecl(_)
        | AstStmt::Break
        | AstStmt::Continue
        | AstStmt::Goto(_)
        | AstStmt::Label(_) | AstStmt::Error(_) => false,
    }
}

fn rewrite_global_decl_use_sites(
    global_decl: &mut AstGlobalDecl,
    candidate: InlineCandidate,
    replacement: &AstExpr,
    options: ReadabilityOptions,
    policy: InlinePolicy,
) -> bool {
    rewrite_expr_list_context(
        &mut global_decl.values,
        candidate,
        replacement,
        InlineSite::Neutral,
        options,
        policy,
    )
}

fn rewrite_expr_list_context(
    exprs: &mut [AstExpr],
    candidate: InlineCandidate,
    replacement: &AstExpr,
    site: InlineSite,
    options: ReadabilityOptions,
    policy: InlinePolicy,
) -> bool {
    let mut changed = false;
    for expr in exprs {
        changed |=
            rewrite_top_level_expr_use_sites(expr, candidate, replacement, site, options, policy);
    }
    changed
}

fn rewrite_lvalue_use_sites(
    lvalue: &mut AstLValue,
    candidate: InlineCandidate,
    replacement: &AstExpr,
    options: ReadabilityOptions,
    policy: InlinePolicy,
) -> bool {
    match lvalue {
        AstLValue::Name(_) => false,
        AstLValue::FieldAccess(access) => rewrite_expr_use_sites(
            &mut access.base,
            candidate,
            replacement,
            InlineSite::Neutral.descend_access_base(),
            options,
            policy,
        ),
        AstLValue::IndexAccess(access) => {
            let mut changed = rewrite_expr_use_sites(
                &mut access.base,
                candidate,
                replacement,
                InlineSite::Neutral.descend_access_base(),
                options,
                policy,
            );
            changed |= rewrite_expr_use_sites(
                &mut access.index,
                candidate,
                replacement,
                InlineSite::Index,
                options,
                policy,
            );
            changed
        }
    }
}

fn rewrite_call_use_sites(
    call: &mut AstCallKind,
    candidate: InlineCandidate,
    replacement: &AstExpr,
    options: ReadabilityOptions,
    policy: InlinePolicy,
) -> bool {
    match call {
        AstCallKind::Call(call) => {
            rewrite_call_expr_use_sites(call, candidate, replacement, options, policy, true)
        }
        AstCallKind::MethodCall(call) => {
            rewrite_method_call_expr_use_sites(call, candidate, replacement, options, policy, true)
        }
    }
}

fn rewrite_top_level_expr_use_sites(
    expr: &mut AstExpr,
    candidate: InlineCandidate,
    replacement: &AstExpr,
    site: InlineSite,
    options: ReadabilityOptions,
    policy: InlinePolicy,
) -> bool {
    match expr {
        AstExpr::Call(call) => {
            rewrite_call_expr_use_sites(call, candidate, replacement, options, policy, true)
        }
        AstExpr::MethodCall(call) => {
            rewrite_method_call_expr_use_sites(call, candidate, replacement, options, policy, true)
        }
        _ => rewrite_expr_use_sites(expr, candidate, replacement, site, options, policy),
    }
}

#[derive(Clone, Copy)]
struct CallRewriteMode {
    options: ReadabilityOptions,
    policy: InlinePolicy,
    allow_raw_global_adjacent_arg_inline: bool,
}

fn rewrite_call_expr_use_sites(
    call: &mut AstCallExpr,
    candidate: InlineCandidate,
    replacement: &AstExpr,
    options: ReadabilityOptions,
    policy: InlinePolicy,
    allow_raw_global_adjacent_arg_inline: bool,
) -> bool {
    rewrite_call_parts_use_sites(
        &mut call.callee,
        &mut call.args,
        InlineSite::CallCallee,
        candidate,
        replacement,
        CallRewriteMode {
            options,
            policy,
            allow_raw_global_adjacent_arg_inline,
        },
    )
}

fn rewrite_method_call_expr_use_sites(
    call: &mut AstMethodCallExpr,
    candidate: InlineCandidate,
    replacement: &AstExpr,
    options: ReadabilityOptions,
    policy: InlinePolicy,
    allow_raw_global_adjacent_arg_inline: bool,
) -> bool {
    rewrite_call_parts_use_sites(
        &mut call.receiver,
        &mut call.args,
        InlineSite::Neutral,
        candidate,
        replacement,
        CallRewriteMode {
            options,
            policy,
            allow_raw_global_adjacent_arg_inline,
        },
    )
}

fn rewrite_call_parts_use_sites(
    prefix: &mut AstExpr,
    args: &mut [AstExpr],
    prefix_site: InlineSite,
    candidate: InlineCandidate,
    replacement: &AstExpr,
    mode: CallRewriteMode,
) -> bool {
    let mut changed = rewrite_expr_use_sites(
        prefix,
        candidate,
        replacement,
        prefix_site,
        mode.options,
        mode.policy,
    );
    let mut prefix_safe = mode.allow_raw_global_adjacent_arg_inline
        && call_prefix_base_allows_raw_global_arg_inline(mode.policy, replacement, prefix);
    let args_len = args.len();
    for (index, arg) in args.iter_mut().enumerate() {
        if prefix_safe && try_rewrite_raw_global_call_arg(arg, candidate, replacement) {
            changed = true;
        } else {
            changed |= rewrite_expr_use_sites(
                arg,
                candidate,
                replacement,
                call_arg_site(index, args_len),
                mode.options,
                mode.policy,
            );
        }
        prefix_safe &= raw_global_call_prefix_expr_is_barrier_free(arg);
    }
    changed
}

fn call_prefix_base_allows_raw_global_arg_inline(
    policy: InlinePolicy,
    replacement: &AstExpr,
    prefix: &AstExpr,
) -> bool {
    matches!(policy, InlinePolicy::AdjacentValueSink)
        && is_raw_global_alias_expr(replacement)
        && raw_global_call_prefix_expr_is_barrier_free(prefix)
}

fn raw_global_call_prefix_expr_is_barrier_free(expr: &AstExpr) -> bool {
    is_access_base_inline_expr(expr) || is_extended_call_arg_local_alias_expr(expr)
}

fn try_rewrite_raw_global_call_arg(
    arg: &mut AstExpr,
    candidate: InlineCandidate,
    replacement: &AstExpr,
) -> bool {
    let AstExpr::Var(name) = arg else {
        return false;
    };
    if !name_matches_binding(name, candidate.binding()) {
        return false;
    }
    *arg = replacement.clone();
    true
}

fn rewrite_expr_use_sites(
    expr: &mut AstExpr,
    candidate: InlineCandidate,
    replacement: &AstExpr,
    site: InlineSite,
    options: ReadabilityOptions,
    policy: InlinePolicy,
) -> bool {
    if site.allows(candidate, expr, replacement, options, policy) {
        *expr = replacement.clone();
        return true;
    }

    match expr {
        AstExpr::FieldAccess(access) => rewrite_expr_use_sites(
            &mut access.base,
            candidate,
            replacement,
            site.descend_access_base(),
            options,
            policy,
        ),
        AstExpr::IndexAccess(access) => {
            let mut changed = rewrite_expr_use_sites(
                &mut access.base,
                candidate,
                replacement,
                site.descend_access_base(),
                options,
                policy,
            );
            changed |= rewrite_expr_use_sites(
                &mut access.index,
                candidate,
                replacement,
                InlineSite::Index,
                options,
                policy,
            );
            changed
        }
        AstExpr::Unary(unary) => rewrite_expr_use_sites(
            &mut unary.expr,
            candidate,
            replacement,
            site.descend_value_expr(),
            options,
            policy,
        ),
        AstExpr::Binary(binary) => {
            let operand_site = match binary.op {
                super::super::super::common::AstBinaryOpKind::Eq
                | super::super::super::common::AstBinaryOpKind::Lt
                | super::super::super::common::AstBinaryOpKind::Le => InlineSite::ComparisonOperand,
                _ => site.descend_value_expr(),
            };
            let mut changed = rewrite_expr_use_sites(
                &mut binary.lhs,
                candidate,
                replacement,
                operand_site,
                options,
                policy,
            );
            changed |= rewrite_expr_use_sites(
                &mut binary.rhs,
                candidate,
                replacement,
                operand_site,
                options,
                policy,
            );
            changed
        }
        AstExpr::LogicalAnd(logical) | AstExpr::LogicalOr(logical) => {
            let mut changed = rewrite_expr_use_sites(
                &mut logical.lhs,
                candidate,
                replacement,
                site.descend_value_expr(),
                options,
                policy,
            );
            changed |= rewrite_expr_use_sites(
                &mut logical.rhs,
                candidate,
                replacement,
                site.descend_value_expr(),
                options,
                policy,
            );
            changed
        }
        AstExpr::Call(call) => {
            rewrite_call_expr_use_sites(call, candidate, replacement, options, policy, false)
        }
        AstExpr::MethodCall(call) => {
            rewrite_method_call_expr_use_sites(call, candidate, replacement, options, policy, false)
        }
        AstExpr::SingleValue(expr) => rewrite_expr_use_sites(
            expr,
            candidate,
            replacement,
            site.descend_value_expr(),
            options,
            policy,
        ),
        AstExpr::TableConstructor(table) => {
            let mut changed = false;
            for field in &mut table.fields {
                match field {
                    AstTableField::Array(value) => {
                        changed |= rewrite_expr_use_sites(
                            value,
                            candidate,
                            replacement,
                            InlineSite::Neutral,
                            options,
                            policy,
                        );
                    }
                    AstTableField::Record(record) => {
                        if let AstTableKey::Expr(key) = &mut record.key {
                            changed |= rewrite_expr_use_sites(
                                key,
                                candidate,
                                replacement,
                                InlineSite::Index,
                                options,
                                policy,
                            );
                        }
                        changed |= rewrite_expr_use_sites(
                            &mut record.value,
                            candidate,
                            replacement,
                            InlineSite::Neutral,
                            options,
                            policy,
                        );
                    }
                }
            }
            changed
        }
        AstExpr::FunctionExpr(_)
        | AstExpr::Nil
        | AstExpr::Boolean(_)
        | AstExpr::Integer(_)
        | AstExpr::Number(_)
        | AstExpr::String(_)
        | AstExpr::Int64(_)
        | AstExpr::UInt64(_)
        | AstExpr::Complex { .. }
        | AstExpr::Var(_)
        | AstExpr::VarArg | AstExpr::Error(_) => false,
    }
}

#[derive(Clone, Copy)]
enum InlineSite {
    Neutral,
    ComparisonOperand,
    ReturnValue,
    ReturnNestedValue,
    Index,
    CallArgNonFinal,
    CallArgFinal,
    CallCallee,
    AccessBase,
}

impl InlineSite {
    fn allows(
        self,
        candidate: InlineCandidate,
        use_expr: &AstExpr,
        replacement: &AstExpr,
        options: ReadabilityOptions,
        policy: InlinePolicy,
    ) -> bool {
        if !matches!(use_expr, AstExpr::Var(name) if name_matches_binding(name, candidate.binding()))
        {
            return false;
        }

        let Some(limit) = self.complexity_limit(options, policy) else {
            return false;
        };
        if expr_complexity(replacement) > limit {
            return false;
        }

        match candidate {
            InlineCandidate::TempLike(_) => match policy {
                InlinePolicy::MechanicalRun => self.allows_mechanical_run_expr(replacement),
                InlinePolicy::DirectReturnConstructor => false,
                _ => {
                    !matches!(self, Self::AccessBase | Self::CallCallee)
                        || is_access_base_inline_expr(replacement)
                }
            },
            InlineCandidate::LocalAlias { origin, .. } => match policy {
                InlinePolicy::Conservative => match origin {
                    super::super::super::common::AstLocalOrigin::DebugHinted => {
                        matches!(self, Self::CallCallee | Self::AccessBase)
                            && is_access_base_inline_expr(replacement)
                    }
                    super::super::super::common::AstLocalOrigin::Recovered => match self {
                        Self::CallCallee | Self::AccessBase => {
                            is_access_base_inline_expr(replacement)
                                || is_lookup_inline_expr(replacement)
                        }
                        Self::ComparisonOperand => {
                            is_access_base_inline_expr(replacement)
                                || is_recallable_inline_expr(replacement)
                        }
                        Self::ReturnNestedValue => {
                            is_recallable_inline_expr(replacement)
                                || is_lookup_inline_expr(replacement)
                        }
                        _ => false,
                    },
                },
                InlinePolicy::ExtendedCallChain => self.allows_extended_local_alias(replacement),
                InlinePolicy::AliasInitializerChain => {
                    self.allows_alias_initializer_local_alias(replacement)
                }
                InlinePolicy::AdjacentCallResultCallee => {
                    self.allows_adjacent_call_result_local_alias(replacement)
                }
                InlinePolicy::AdjacentValueSink => match origin {
                    super::super::super::common::AstLocalOrigin::DebugHinted => false,
                    super::super::super::common::AstLocalOrigin::Recovered => {
                        self.allows_adjacent_value_sink_local_alias(replacement)
                    }
                },
                InlinePolicy::DirectReturnConstructor => match origin {
                    super::super::super::common::AstLocalOrigin::DebugHinted => false,
                    super::super::super::common::AstLocalOrigin::Recovered => {
                        self.allows_direct_return_constructor_local_alias(replacement)
                    }
                },
                InlinePolicy::MechanicalRun => match origin {
                    super::super::super::common::AstLocalOrigin::DebugHinted => false,
                    super::super::super::common::AstLocalOrigin::Recovered => {
                        self.allows_mechanical_run_expr(replacement)
                    }
                },
            },
        }
    }

    fn complexity_limit(self, options: ReadabilityOptions, policy: InlinePolicy) -> Option<usize> {
        match self {
            Self::Neutral => match policy {
                InlinePolicy::AliasInitializerChain => {
                    Some(options.access_base_inline_max_complexity)
                }
                InlinePolicy::AdjacentCallResultCallee => None,
                InlinePolicy::AdjacentValueSink => Some(options.return_inline_max_complexity),
                InlinePolicy::Conservative => None,
                InlinePolicy::DirectReturnConstructor => None,
                InlinePolicy::ExtendedCallChain => Some(options.access_base_inline_max_complexity),
                InlinePolicy::MechanicalRun => Some(options.return_inline_max_complexity),
            },
            Self::ComparisonOperand => Some(options.args_inline_max_complexity),
            Self::ReturnValue => match policy {
                InlinePolicy::DirectReturnConstructor => Some(usize::MAX),
                _ => Some(options.return_inline_max_complexity),
            },
            Self::ReturnNestedValue => Some(options.return_inline_max_complexity),
            Self::Index => Some(options.index_inline_max_complexity),
            Self::CallArgNonFinal | Self::CallArgFinal => Some(options.args_inline_max_complexity),
            // 这里刻意复用 access-base 的阈值:
            // `table.concat(tbl)` 这类“把别名还原回前缀表达式”的可读性取舍,
            // 本质上和 `obj[key]` 里的 base 折叠是同一种源码形状决策。
            Self::CallCallee => Some(options.access_base_inline_max_complexity),
            Self::AccessBase => Some(options.access_base_inline_max_complexity),
        }
    }

    fn descend_access_base(self) -> Self {
        match self {
            Self::Neutral => Self::AccessBase,
            Self::ComparisonOperand => Self::ComparisonOperand,
            Self::ReturnValue => Self::ReturnNestedValue,
            Self::ReturnNestedValue => Self::ReturnNestedValue,
            // 调用参数中的 field access base 用 AccessBase 而非 Neutral:
            // `f(r.KEY)` 内联 `r = T.F` 只是把 call arg 从 `r.KEY` 延长成 `T.F.KEY`,
            // 仍然是同类型的命名字段链,不会引入新的副作用或可读性退化。
            Self::CallArgNonFinal | Self::CallArgFinal => Self::AccessBase,
            Self::Index | Self::AccessBase => Self::Neutral,
            Self::CallCallee => Self::CallCallee,
        }
    }

    fn descend_value_expr(self) -> Self {
        match self {
            Self::ReturnValue | Self::ReturnNestedValue => Self::ReturnNestedValue,
            Self::ComparisonOperand => Self::ComparisonOperand,
            Self::Neutral
            | Self::Index
            | Self::CallArgNonFinal
            | Self::CallArgFinal
            | Self::CallCallee
            | Self::AccessBase => Self::Neutral,
        }
    }

    fn allows_extended_local_alias(self, replacement: &AstExpr) -> bool {
        match self {
            Self::Neutral => is_extended_neutral_local_alias_expr(replacement),
            Self::ComparisonOperand => {
                is_extended_neutral_local_alias_expr(replacement)
                    || is_recallable_inline_expr(replacement)
            }
            Self::ReturnNestedValue => {
                is_recallable_inline_expr(replacement) || is_lookup_inline_expr(replacement)
            }
            Self::CallCallee => is_call_callee_inline_expr(replacement),
            Self::CallArgNonFinal => {
                is_extended_call_arg_local_alias_expr(replacement)
                    || is_recallable_inline_expr(replacement)
            }
            // 这里只有在“局部别名包折回最终调用”时,才允许把纯 lookup 收回参数位。
            // 这能把 `local x = t[1]; local y = t.a; print(x, y)` 这类机械展开收回去,
            // 同时仍然不放宽到任意调用结果,避免把阶段 local 继续吞掉。
            Self::CallArgFinal => is_extended_call_arg_local_alias_expr(replacement),
            Self::AccessBase => is_access_base_inline_expr(replacement),
            Self::ReturnValue | Self::Index => false,
        }
    }

    fn allows_alias_initializer_local_alias(self, replacement: &AstExpr) -> bool {
        match self {
            // 这里专门服务“局部别名链初始化”:
            // `local unpack = table.unpack; local fn = unpack or _G.unpack`
            // 这种形状本质上还是在组装一个后续调用会消费的前缀表达式别名。
            // 允许它在紧邻的下一条 local alias 初始化式里收回,能把机械拆分重新压回
            // 更接近源码的单条声明,而不会放宽到普通 return/if/赋值上下文。
            Self::Neutral | Self::ComparisonOperand | Self::CallCallee => {
                is_access_base_inline_expr(replacement)
            }
            // 这里额外允许 lookup 落到 access base:
            // `local item = items[i]; local weight = item.weight`
            // 仍然只是把“取前缀再取字段”的机械两段式收回同一条 local 初始化。
            Self::AccessBase => {
                is_access_base_inline_expr(replacement) || is_lookup_inline_expr(replacement)
            }
            Self::ReturnValue
            | Self::ReturnNestedValue
            | Self::Index
            | Self::CallArgNonFinal
            | Self::CallArgFinal => false,
        }
    }

    fn allows_adjacent_call_result_local_alias(self, replacement: &AstExpr) -> bool {
        matches!(self, Self::CallCallee) && is_lookup_inline_expr(replacement)
    }

    fn allows_adjacent_value_sink_local_alias(self, replacement: &AstExpr) -> bool {
        match self {
            Self::Neutral | Self::ComparisonOperand => {
                is_extended_neutral_local_alias_expr(replacement)
                    || is_recallable_inline_expr(replacement)
            }
            Self::CallArgNonFinal | Self::CallArgFinal => {
                is_extended_call_arg_local_alias_expr(replacement)
                    || is_recallable_inline_expr(replacement)
            }
            Self::ReturnNestedValue => {
                is_recallable_inline_expr(replacement) || is_lookup_inline_expr(replacement)
            }
            Self::AccessBase => {
                is_access_base_inline_expr(replacement) || is_lookup_inline_expr(replacement)
            }
            Self::CallCallee => is_call_callee_inline_expr(replacement),
            Self::ReturnValue | Self::Index => false,
        }
    }

    fn allows_direct_return_constructor_local_alias(self, replacement: &AstExpr) -> bool {
        matches!(self, Self::ReturnValue) && is_direct_return_constructor_inline_expr(replacement)
    }

    fn allows_mechanical_run_expr(self, replacement: &AstExpr) -> bool {
        match self {
            Self::Neutral | Self::ComparisonOperand | Self::ReturnNestedValue | Self::Index => {
                is_mechanical_run_inline_expr(replacement)
            }
            Self::CallCallee => is_call_callee_inline_expr(replacement),
            Self::AccessBase => {
                is_access_base_inline_expr(replacement) || is_lookup_inline_expr(replacement)
            }
            Self::ReturnValue | Self::CallArgNonFinal | Self::CallArgFinal => false,
        }
    }
}

fn call_arg_site(index: usize, len: usize) -> InlineSite {
    if index + 1 == len {
        InlineSite::CallArgFinal
    } else {
        InlineSite::CallArgNonFinal
    }
}