rue_compiler/compile/item/
module.rs

1use rue_ast::AstModuleItem;
2use rue_diagnostic::DiagnosticKind;
3use rue_hir::{ModuleDeclarations, ModuleSymbol, Scope, Symbol, SymbolId};
4
5use crate::{
6    Compiler, compile_symbol_items, compile_type_items, declare_symbol_items, declare_type_items,
7};
8
9pub fn declare_module_types(ctx: &mut Compiler, module: &AstModuleItem) -> SymbolId {
10    let scope = ctx.alloc_scope(Scope::new());
11
12    let mut declarations = ModuleDeclarations::default();
13    declare_type_items(ctx, scope, module.items(), &mut declarations);
14
15    let symbol = ctx.alloc_symbol(Symbol::Module(ModuleSymbol {
16        name: module.name(),
17        scope,
18        declarations,
19    }));
20
21    if let Some(name) = module.name() {
22        if ctx.last_scope().symbol(name.text()).is_some() {
23            ctx.diagnostic(
24                &name,
25                DiagnosticKind::DuplicateSymbol(name.text().to_string()),
26            );
27        }
28
29        ctx.last_scope_mut().insert_symbol(
30            name.text().to_string(),
31            symbol,
32            module.export().is_some(),
33        );
34    }
35
36    symbol
37}
38
39pub fn declare_module_symbols(ctx: &mut Compiler, module: &AstModuleItem, symbol: SymbolId) {
40    let (scope, mut declarations) = if let Symbol::Module(ModuleSymbol {
41        scope,
42        declarations,
43        ..
44    }) = ctx.symbol(symbol)
45    {
46        (*scope, declarations.clone())
47    } else {
48        unreachable!();
49    };
50
51    declare_symbol_items(ctx, scope, module.items(), &mut declarations);
52
53    let Symbol::Module(ModuleSymbol {
54        declarations: updated,
55        ..
56    }) = ctx.symbol_mut(symbol)
57    else {
58        unreachable!();
59    };
60
61    *updated = declarations;
62}
63
64pub fn compile_module_types(ctx: &mut Compiler, module: &AstModuleItem, symbol: SymbolId) {
65    let (scope, declarations) = if let Symbol::Module(ModuleSymbol {
66        scope,
67        declarations,
68        ..
69    }) = ctx.symbol(symbol)
70    {
71        (*scope, declarations.clone())
72    } else {
73        unreachable!();
74    };
75
76    compile_type_items(ctx, scope, module.items(), &declarations);
77}
78
79pub fn compile_module_symbols(ctx: &mut Compiler, module: &AstModuleItem, symbol: SymbolId) {
80    let (scope, declarations) = if let Symbol::Module(ModuleSymbol {
81        scope,
82        declarations,
83        ..
84    }) = ctx.symbol(symbol)
85    {
86        (*scope, declarations.clone())
87    } else {
88        unreachable!();
89    };
90
91    compile_symbol_items(ctx, scope, module.items(), &declarations);
92}