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
//! 这个文件负责从 AST 重建 Naming 需要的词法可见域信息。
//!
//! Naming 不能假定前层已经单独导出 scope facts,所以这里显式记录:
//! “某个函数在定义点到底能看到哪些外层绑定”。这样后续参数命名就能避开
//! 祖先作用域里当前可见的自动生成名字,而不是退化成全局禁名或拍脑袋加后缀。

use crate::ast::{
    AstBindingRef, AstBlock, AstCallKind, AstExpr, AstFunctionExpr, AstLValue,
    AstLocalDecl, AstModule, AstStmt, AstSyntheticLocalId,
};
use crate::ast::traverse::{
    traverse_call_children, traverse_expr_children, traverse_lvalue_children,
};
use crate::hir::{HirModule, HirProtoRef, LocalId, ParamId, UpvalueId};

use super::NamingError;

/// 按函数记录其定义点能看到的外层绑定。
#[derive(Debug, Clone, Default)]
pub(crate) struct LexicalContexts {
    pub(crate) functions: Vec<FunctionLexicalContext>,
}

impl LexicalContexts {
    pub(crate) fn function(&self, function: HirProtoRef) -> Option<&FunctionLexicalContext> {
        self.functions.get(function.index())
    }
}

/// 单个函数的词法上下文。
#[derive(Debug, Clone, Default)]
pub(crate) struct FunctionLexicalContext {
    pub(crate) outer_visible_bindings: Vec<VisibleBinding>,
}

/// 在函数定义点可见的外层绑定。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub(crate) enum VisibleBinding {
    Param {
        function: HirProtoRef,
        param: ParamId,
    },
    Local {
        function: HirProtoRef,
        local: LocalId,
    },
    SyntheticLocal {
        function: HirProtoRef,
        local: AstSyntheticLocalId,
    },
    Upvalue {
        function: HirProtoRef,
        upvalue: UpvalueId,
    },
}

/// 从 AST 结构推导词法上下文。
pub(crate) fn collect_lexical_contexts(
    module: &AstModule,
    hir: &HirModule,
) -> Result<LexicalContexts, NamingError> {
    let mut contexts = LexicalContexts {
        functions: vec![FunctionLexicalContext::default(); hir.protos.len()],
    };
    collect_function_context(module.entry_function, &module.body, hir, &mut contexts, &[])?;
    Ok(contexts)
}

fn collect_function_context(
    function: HirProtoRef,
    body: &AstBlock,
    hir: &HirModule,
    contexts: &mut LexicalContexts,
    outer_visible_bindings: &[VisibleBinding],
) -> Result<(), NamingError> {
    let Some(proto) = hir.protos.get(function.index()) else {
        return Err(NamingError::MissingFunction {
            function: function.index(),
        });
    };
    contexts.functions[function.index()].outer_visible_bindings = outer_visible_bindings.to_vec();

    let mut scopes = vec![Vec::new()];
    for &param in &proto.params {
        declare_binding(&mut scopes, VisibleBinding::Param { function, param });
    }
    if proto.signature.has_vararg_param_reg
        && let Some(&local) = proto.locals.first()
    {
        declare_binding(&mut scopes, VisibleBinding::Local { function, local });
    }
    for &upvalue in &proto.upvalues {
        declare_binding(&mut scopes, VisibleBinding::Upvalue { function, upvalue });
    }

    collect_block_context(
        function,
        body,
        hir,
        contexts,
        outer_visible_bindings,
        &mut scopes,
    )
}

fn collect_block_context(
    function: HirProtoRef,
    block: &AstBlock,
    hir: &HirModule,
    contexts: &mut LexicalContexts,
    outer_visible_bindings: &[VisibleBinding],
    scopes: &mut Vec<Vec<VisibleBinding>>,
) -> Result<(), NamingError> {
    for stmt in &block.stmts {
        collect_stmt_context(
            function,
            stmt,
            hir,
            contexts,
            outer_visible_bindings,
            scopes,
        )?;
    }
    Ok(())
}

fn collect_stmt_context(
    function: HirProtoRef,
    stmt: &AstStmt,
    hir: &HirModule,
    contexts: &mut LexicalContexts,
    outer_visible_bindings: &[VisibleBinding],
    scopes: &mut Vec<Vec<VisibleBinding>>,
) -> Result<(), NamingError> {
    match stmt {
        AstStmt::LocalDecl(local_decl) => {
            collect_local_decl_context(
                function,
                local_decl,
                hir,
                contexts,
                outer_visible_bindings,
                scopes,
            )?;
        }
        AstStmt::GlobalDecl(global_decl) => {
            for value in &global_decl.values {
                collect_expr_context(value, hir, contexts, outer_visible_bindings, scopes)?;
            }
        }
        AstStmt::Assign(assign) => {
            for target in &assign.targets {
                collect_lvalue_context(target, hir, contexts, outer_visible_bindings, scopes)?;
            }
            for value in &assign.values {
                collect_expr_context(value, hir, contexts, outer_visible_bindings, scopes)?;
            }
        }
        AstStmt::CallStmt(call_stmt) => {
            collect_call_context(
                &call_stmt.call,
                hir,
                contexts,
                outer_visible_bindings,
                scopes,
            )?;
        }
        AstStmt::Return(ret) => {
            for value in &ret.values {
                collect_expr_context(value, hir, contexts, outer_visible_bindings, scopes)?;
            }
        }
        AstStmt::If(if_stmt) => {
            collect_expr_context(&if_stmt.cond, hir, contexts, outer_visible_bindings, scopes)?;
            with_nested_scope(scopes, |scopes| {
                collect_block_context(
                    function,
                    &if_stmt.then_block,
                    hir,
                    contexts,
                    outer_visible_bindings,
                    scopes,
                )
            })?;
            if let Some(else_block) = &if_stmt.else_block {
                with_nested_scope(scopes, |scopes| {
                    collect_block_context(
                        function,
                        else_block,
                        hir,
                        contexts,
                        outer_visible_bindings,
                        scopes,
                    )
                })?;
            }
        }
        AstStmt::While(while_stmt) => {
            collect_expr_context(
                &while_stmt.cond,
                hir,
                contexts,
                outer_visible_bindings,
                scopes,
            )?;
            with_nested_scope(scopes, |scopes| {
                collect_block_context(
                    function,
                    &while_stmt.body,
                    hir,
                    contexts,
                    outer_visible_bindings,
                    scopes,
                )
            })?;
        }
        AstStmt::Repeat(repeat_stmt) => {
            // `repeat ... until cond` 的条件仍处在同一个词法块里。
            // 这里不能像 while 一样先跑 body 再弹 scope,否则会丢掉 body 中局部对 cond 的可见性。
            with_nested_scope(scopes, |scopes| {
                collect_block_context(
                    function,
                    &repeat_stmt.body,
                    hir,
                    contexts,
                    outer_visible_bindings,
                    scopes,
                )?;
                collect_expr_context(
                    &repeat_stmt.cond,
                    hir,
                    contexts,
                    outer_visible_bindings,
                    scopes,
                )
            })?;
        }
        AstStmt::NumericFor(numeric_for) => {
            collect_expr_context(
                &numeric_for.start,
                hir,
                contexts,
                outer_visible_bindings,
                scopes,
            )?;
            collect_expr_context(
                &numeric_for.limit,
                hir,
                contexts,
                outer_visible_bindings,
                scopes,
            )?;
            collect_expr_context(
                &numeric_for.step,
                hir,
                contexts,
                outer_visible_bindings,
                scopes,
            )?;
            with_nested_scope(scopes, |scopes| {
                declare_ast_binding(function, numeric_for.binding, scopes);
                collect_block_context(
                    function,
                    &numeric_for.body,
                    hir,
                    contexts,
                    outer_visible_bindings,
                    scopes,
                )
            })?;
        }
        AstStmt::GenericFor(generic_for) => {
            for expr in &generic_for.iterator {
                collect_expr_context(expr, hir, contexts, outer_visible_bindings, scopes)?;
            }
            with_nested_scope(scopes, |scopes| {
                for &binding in &generic_for.bindings {
                    declare_ast_binding(function, binding, scopes);
                }
                collect_block_context(
                    function,
                    &generic_for.body,
                    hir,
                    contexts,
                    outer_visible_bindings,
                    scopes,
                )
            })?;
        }
        AstStmt::DoBlock(block) => {
            with_nested_scope(scopes, |scopes| {
                collect_block_context(
                    function,
                    block,
                    hir,
                    contexts,
                    outer_visible_bindings,
                    scopes,
                )
            })?;
        }
        AstStmt::FunctionDecl(function_decl) => {
            collect_nested_function_context(
                &function_decl.func,
                hir,
                contexts,
                outer_visible_bindings,
                scopes,
            )?;
        }
        AstStmt::LocalFunctionDecl(local_function_decl) => {
            // `local function f() ... end` 里的 `f` 在函数体内也是可见的,
            // 所以要先把它放进当前作用域,再收集子函数的词法上下文。
            declare_ast_binding(function, local_function_decl.name, scopes);
            collect_nested_function_context(
                &local_function_decl.func,
                hir,
                contexts,
                outer_visible_bindings,
                scopes,
            )?;
        }
        AstStmt::Break | AstStmt::Continue | AstStmt::Goto(_) | AstStmt::Label(_) | AstStmt::Error(_) => {}
    }
    Ok(())
}

fn collect_local_decl_context(
    function: HirProtoRef,
    local_decl: &AstLocalDecl,
    hir: &HirModule,
    contexts: &mut LexicalContexts,
    outer_visible_bindings: &[VisibleBinding],
    scopes: &mut Vec<Vec<VisibleBinding>>,
) -> Result<(), NamingError> {
    for value in &local_decl.values {
        collect_expr_context(value, hir, contexts, outer_visible_bindings, scopes)?;
    }
    for binding in &local_decl.bindings {
        declare_ast_binding(function, binding.id, scopes);
    }
    Ok(())
}

fn collect_nested_function_context(
    function_expr: &AstFunctionExpr,
    hir: &HirModule,
    contexts: &mut LexicalContexts,
    outer_visible_bindings: &[VisibleBinding],
    scopes: &[Vec<VisibleBinding>],
) -> Result<(), NamingError> {
    let child_outer_visible = visible_snapshot(outer_visible_bindings, scopes);
    collect_function_context(
        function_expr.function,
        &function_expr.body,
        hir,
        contexts,
        &child_outer_visible,
    )
}

fn collect_call_context(
    call: &AstCallKind,
    hir: &HirModule,
    contexts: &mut LexicalContexts,
    outer_visible_bindings: &[VisibleBinding],
    scopes: &mut Vec<Vec<VisibleBinding>>,
) -> Result<(), NamingError> {
    traverse_call_children!(call, iter = iter, borrow = [&], expr(expr) => {
        collect_expr_context(expr, hir, contexts, outer_visible_bindings, scopes)?;
    });
    Ok(())
}

fn collect_lvalue_context(
    target: &AstLValue,
    hir: &HirModule,
    contexts: &mut LexicalContexts,
    outer_visible_bindings: &[VisibleBinding],
    scopes: &mut Vec<Vec<VisibleBinding>>,
) -> Result<(), NamingError> {
    traverse_lvalue_children!(target, borrow = [&], expr(expr) => {
        collect_expr_context(expr, hir, contexts, outer_visible_bindings, scopes)?;
    });
    Ok(())
}

fn collect_expr_context(
    expr: &AstExpr,
    hir: &HirModule,
    contexts: &mut LexicalContexts,
    outer_visible_bindings: &[VisibleBinding],
    scopes: &mut Vec<Vec<VisibleBinding>>,
) -> Result<(), NamingError> {
    traverse_expr_children!(
        expr,
        iter = iter,
        borrow = [&],
        expr(child) => {
            collect_expr_context(child, hir, contexts, outer_visible_bindings, scopes)?;
        },
        function(func) => {
            collect_nested_function_context(func, hir, contexts, outer_visible_bindings, scopes)?;
        }
    );
    Ok(())
}

fn declare_ast_binding(
    function: HirProtoRef,
    binding: AstBindingRef,
    scopes: &mut [Vec<VisibleBinding>],
) {
    match binding {
        AstBindingRef::Local(local) => {
            declare_binding(scopes, VisibleBinding::Local { function, local });
        }
        AstBindingRef::SyntheticLocal(local) => {
            declare_binding(scopes, VisibleBinding::SyntheticLocal { function, local });
        }
        AstBindingRef::Temp(_) => unreachable!(
            "readability output must not leak raw temp bindings into naming lexical analysis"
        ),
    }
}

fn declare_binding(scopes: &mut [Vec<VisibleBinding>], binding: VisibleBinding) {
    scopes
        .last_mut()
        .expect("lexical context must always keep at least one scope")
        .push(binding);
}

fn visible_snapshot(
    outer_visible_bindings: &[VisibleBinding],
    scopes: &[Vec<VisibleBinding>],
) -> Vec<VisibleBinding> {
    let mut visible = Vec::with_capacity(
        outer_visible_bindings.len() + scopes.iter().map(Vec::len).sum::<usize>(),
    );
    visible.extend_from_slice(outer_visible_bindings);
    for scope in scopes {
        visible.extend_from_slice(scope);
    }
    visible
}

fn with_nested_scope<T, F>(scopes: &mut Vec<Vec<VisibleBinding>>, f: F) -> Result<T, NamingError>
where
    F: FnOnce(&mut Vec<Vec<VisibleBinding>>) -> Result<T, NamingError>,
{
    scopes.push(Vec::new());
    let result = f(scopes);
    let popped = scopes.pop();
    debug_assert!(popped.is_some(), "nested lexical scope should exist");
    result
}