slang_frontend/semantic_analysis/analyzer_modules/native_functions.rs
1use slang_shared::{CompilationContext, SymbolKind};
2use slang_types::TypeId;
3
4/// Registers the built-in native functions that are available to all programs.
5pub fn register_native_functions(context: &mut CompilationContext) {
6 // Register print_value function
7 // It accepts any type (TypeId::unknown() can represent this for now)
8 // and returns an i32 (e.g., status code, though not strictly enforced here).
9 let param_types = vec![TypeId::unknown()];
10 let return_type = TypeId::i32();
11
12 let function_type_id = context.register_function_type(param_types, return_type);
13
14 // Define the 'print_value' symbol as a function.
15 // The 'false' indicates it's not mutable, which is typical for functions.
16 if context
17 .define_symbol(
18 "print_value".to_string(),
19 SymbolKind::Function,
20 function_type_id,
21 false,
22 )
23 .is_err()
24 {
25 // This would ideally log an error or panic if registration fails,
26 // as it indicates a fundamental issue with the compilation context setup.
27 // For now, we'll let it pass, but in a production compiler, this should be handled.
28 eprintln!("Error: Failed to register native function 'print_value'.");
29 }
30
31 // Add other native functions here in the future
32 // Example:
33 // let len_param_types = vec![TypeId::string()]; // Assuming a string type ID
34 // let len_return_type = TypeId::i64(); // Or u64, i32 etc.
35 // let len_function_type_id = context.register_function_type(len_param_types, len_return_type);
36 // if context.define_symbol(
37 // "len".to_string(),
38 // SymbolKind::Function,
39 // len_function_type_id,
40 // false,
41 // ).is_err() {
42 // eprintln!("Error: Failed to register native function 'len'.");
43 // }
44}