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
use std::fs::{create_dir_all, File};
use std::io::{BufWriter, Write};
use std::path::Path;

use erg_common::pathutil::{mod_name, NormalizedPathBuf};
use erg_common::set::Set;
use erg_common::traits::LimitedDisplay;
use erg_common::{log, Str};
use erg_compiler::build_package::{CheckStatus, PylyzerStatus};
use erg_compiler::hir::{ClassDef, Expr, HIR};
use erg_compiler::module::SharedModuleCache;
use erg_compiler::ty::value::{GenTypeObj, TypeObj};
use erg_compiler::ty::{HasType, Type};

pub struct DeclFile {
    pub filename: String,
    pub code: String,
}

fn escape_type(typ: String) -> String {
    typ.replace('%', "Type_").replace("<module>", "")
}

pub struct DeclFileGenerator {
    filename: String,
    namespace: String,
    imported: Set<Str>,
    code: String,
}

impl DeclFileGenerator {
    pub fn new(path: &NormalizedPathBuf, status: CheckStatus) -> Self {
        let (timestamp, hash) = {
            let metadata = std::fs::metadata(path).unwrap();
            let dummy_hash = metadata.len();
            (metadata.modified().unwrap(), dummy_hash)
        };
        let status = PylyzerStatus {
            status,
            file: path.to_path_buf(),
            timestamp,
            hash,
        };
        let code = format!("{status}\n");
        Self {
            filename: path
                .file_name()
                .unwrap()
                .to_string_lossy()
                .replace(".py", ".d.er"),
            namespace: "".to_string(),
            imported: Set::new(),
            code,
        }
    }

    pub fn gen_decl_er(mut self, hir: &HIR) -> DeclFile {
        for chunk in hir.module.iter() {
            self.gen_chunk_decl(chunk);
        }
        log!("code:\n{}", self.code);
        DeclFile {
            filename: self.filename,
            code: self.code,
        }
    }

    // e.g. `x: foo.Bar` => `foo = pyimport "foo"; x: foo.Bar`
    fn prepare_using_type(&mut self, typ: &Type) {
        let namespace = Str::rc(typ.namespace().split('.').next().unwrap());
        if !namespace.is_empty() && self.imported.insert(namespace.clone()) {
            self.code += &format!("{namespace} = pyimport \"{namespace}\"\n");
        }
    }

    fn gen_chunk_decl(&mut self, chunk: &Expr) {
        match chunk {
            Expr::Def(def) => {
                let mut name = def
                    .sig
                    .ident()
                    .inspect()
                    .replace('\0', "")
                    .replace(['%', '*'], "___");
                let ref_t = def.sig.ident().ref_t();
                self.prepare_using_type(ref_t);
                let typ = escape_type(ref_t.replace_failure().to_string_unabbreviated());
                // Erg can automatically import nested modules
                // `import http.client` => `http = pyimport "http"`
                let decl = if ref_t.is_py_module() {
                    name = name.split('.').next().unwrap().to_string();
                    let full_path_str = ref_t.typarams()[0].to_string_unabbreviated();
                    let mod_name = mod_name(Path::new(full_path_str.trim_matches('"')));
                    let imported = if self.imported.insert(mod_name.clone()) {
                        format!("{}.{mod_name} = pyimport \"{mod_name}\"", self.namespace)
                    } else {
                        "".to_string()
                    };
                    if self.imported.insert(name.clone().into()) {
                        format!(
                            "{}.{name} = pyimport \"{mod_name}\"\n{imported}",
                            self.namespace,
                        )
                    } else {
                        imported
                    }
                } else {
                    format!("{}.{name}: {typ}", self.namespace)
                };
                self.code += &decl;
            }
            Expr::ClassDef(def) => {
                let class_name = def
                    .sig
                    .ident()
                    .inspect()
                    .replace('\0', "")
                    .replace(['%', '*'], "___");
                let src = format!("{}.{class_name}", self.namespace);
                let stash = std::mem::replace(&mut self.namespace, src);
                let decl = format!(".{class_name}: ClassType");
                self.code += &decl;
                self.code.push('\n');
                if let GenTypeObj::Subclass(class) = &def.obj {
                    let sup = class
                        .sup
                        .as_ref()
                        .typ()
                        .replace_failure()
                        .to_string_unabbreviated();
                    self.prepare_using_type(class.sup.typ());
                    let sup = escape_type(sup);
                    let decl = format!(".{class_name} <: {sup}\n");
                    self.code += &decl;
                }
                if let Some(TypeObj::Builtin {
                    t: Type::Record(rec),
                    ..
                }) = def.obj.base_or_sup()
                {
                    for (attr, t) in rec.iter() {
                        self.prepare_using_type(t);
                        let typ = escape_type(t.replace_failure().to_string_unabbreviated());
                        let decl = format!("{}.{}: {typ}\n", self.namespace, attr.symbol);
                        self.code += &decl;
                    }
                }
                if let Some(TypeObj::Builtin {
                    t: Type::Record(rec),
                    ..
                }) = def.obj.additional()
                {
                    for (attr, t) in rec.iter() {
                        self.prepare_using_type(t);
                        let typ = escape_type(t.replace_failure().to_string_unabbreviated());
                        let decl = format!("{}.{}: {typ}\n", self.namespace, attr.symbol);
                        self.code += &decl;
                    }
                }
                for attr in ClassDef::get_all_methods(&def.methods_list) {
                    self.gen_chunk_decl(attr);
                }
                self.namespace = stash;
            }
            Expr::Dummy(dummy) => {
                for chunk in dummy.iter() {
                    self.gen_chunk_decl(chunk);
                }
            }
            _ => {}
        }
        self.code.push('\n');
    }
}

fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) {
    let decl_gen = DeclFileGenerator::new(path, status);
    let file = decl_gen.gen_decl_er(hir);
    let Some(dir) = path.parent().and_then(|p| p.canonicalize().ok()) else {
        return;
    };
    let cache_dir = dir.join("__pycache__");
    if !cache_dir.exists() {
        let _ = create_dir_all(&cache_dir);
    }
    let path = cache_dir.join(file.filename);
    if !path.exists() {
        let _f = File::create(&path);
    }
    let Ok(f) = File::options().write(true).open(path) else {
        return;
    };
    let mut f = BufWriter::new(f);
    let _ = f.write_all(file.code.as_bytes());
}

pub fn dump_decl_package(modules: &SharedModuleCache) {
    for (path, module) in modules.raw_iter() {
        if let Some(hir) = module.hir.as_ref() {
            dump_decl_er(path, hir, module.status);
        }
    }
}