Skip to main content

uika_codegen/cpp_gen/
fill_table.rs

1// C++ UikaFillFuncTable.cpp generation.
2
3use std::collections::BTreeMap;
4
5use crate::context::FuncEntry;
6use crate::cpp_gen::wrapper::cpp_wrapper_name;
7
8/// Generate the UikaFillFuncTable.cpp file.
9pub fn generate_fill_table(
10    entries: &[FuncEntry],
11    _by_class: &BTreeMap<(String, String), Vec<&FuncEntry>>,
12) -> String {
13    let mut out = String::with_capacity(entries.len() * 80 + 1024);
14
15    out.push_str("// Auto-generated by uika-codegen. Do not edit.\n\n");
16    out.push_str("#include \"UikaFuncIds.h\"\n\n");
17
18    // Forward-declare all wrapper functions
19    out.push_str("// Forward declarations of wrapper functions\n");
20    for entry in entries {
21        let c_name = cpp_wrapper_name(&entry.class_name, &entry.func_name);
22        out.push_str(&format!("extern \"C\" uint32_t {c_name}(...);\n"));
23    }
24    out.push('\n');
25
26    // Global function table
27    out.push_str(&format!(
28        "static void* GUikaFuncTable[UikaFuncId::FUNC_COUNT];\n\n"
29    ));
30
31    // Fill function
32    out.push_str("void UikaFillFuncTable() {\n");
33    for entry in entries {
34        let const_name =
35            crate::rust_gen::func_ids::func_id_const_name(&entry.class_name, &entry.func_name);
36        let c_name = cpp_wrapper_name(&entry.class_name, &entry.func_name);
37        out.push_str(&format!(
38            "    GUikaFuncTable[UikaFuncId::{const_name}] = (void*)&{c_name};\n"
39        ));
40    }
41    out.push_str("}\n\n");
42
43    // Getter for the table pointer
44    out.push_str("void** UikaGetFuncTable() {\n");
45    out.push_str("    return GUikaFuncTable;\n");
46    out.push_str("}\n\n");
47
48    out.push_str(&format!(
49        "uint32_t UikaGetFuncCount() {{\n    return UikaFuncId::FUNC_COUNT;\n}}\n"
50    ));
51
52    out
53}