use super::{ExportTable, join_path};
use crate::{
analysis::{ExternalFunction, ExternalModuleExport, SymbolKind},
types::FunctionSignature,
};
use std::collections::{HashMap, HashSet};
pub fn extend_external_declarations(
exports: &mut ExportTable,
host: &HashMap<String, FunctionSignature>,
host_types: &HashSet<String>,
) {
super::types::extend_builtin_types(exports, host_types);
for path in host_types {
let (module, name) = path.rsplit_once("::").unwrap_or(("", path));
ensure_module(exports, module, host_types);
let members = exports.entry(module.into()).or_default();
if !members.iter().any(|member| member.name == name) {
members.push(ExternalModuleExport {
canonical_path: path.clone(),
visibility: super::ExportVisibility::Public,
declaration_visibility: super::ExportVisibility::Public,
name: name.into(),
target_module: None,
span: None,
definition_id: None,
external_function: None,
kind: SymbolKind::Type,
inferred_type: Some(crate::types::Type::named(path)),
detail: None,
module_path: module.into(),
fields: Vec::new(),
});
}
}
for declaration in rils_builtins::BUILTINS {
if declaration.kind == rils_builtins::BuiltinKind::Function
&& let Some(signature) =
crate::standard_library::standard_function_signature(declaration.path)
{
insert(
exports,
declaration.path,
signature,
ExternalFunction::Builtin(declaration.path.into()),
Some(declaration.documentation.into()),
host_types,
);
}
}
for (path, signature) in host {
if rils_builtins::builtin_function(path).is_none() {
insert(
exports,
path,
signature.clone(),
ExternalFunction::Host(path.clone()),
None,
host_types,
);
}
}
}
fn insert(
exports: &mut ExportTable,
path: &str,
signature: FunctionSignature,
identity: ExternalFunction,
detail: Option<String>,
host_types: &HashSet<String>,
) {
let (module, name) = path.rsplit_once("::").unwrap_or(("", path));
ensure_module(exports, module, host_types);
let members = exports.entry(module.into()).or_default();
if members.iter().any(|export| export.name == name) {
return;
}
let label = match &identity {
ExternalFunction::Host(path) => format!("host fn {path}"),
ExternalFunction::Builtin(_) => format!("fn {name}"),
};
let detail = Some(format!(
"{label}{}{}",
signature.as_type().to_string().trim_start_matches("fn"),
detail
.filter(|text| !text.is_empty())
.map(|text| format!("\n\n{text}"))
.unwrap_or_default()
));
members.push(ExternalModuleExport {
canonical_path: path.into(),
visibility: super::ExportVisibility::Public,
declaration_visibility: super::ExportVisibility::Public,
name: name.into(),
target_module: None,
span: None,
definition_id: None,
external_function: Some(identity),
kind: SymbolKind::Function,
inferred_type: Some(signature.as_type()),
detail,
module_path: module.into(),
fields: Vec::new(),
});
}
pub(super) fn ensure_module(exports: &mut ExportTable, module: &str, host_types: &HashSet<String>) {
let mut parent = String::new();
for segment in module.split("::").filter(|part| !part.is_empty()) {
let target = join_path(&parent, segment);
let members = exports.entry(parent.clone()).or_default();
if !host_types.contains(&target) && !members.iter().any(|export| export.name == segment) {
members.push(ExternalModuleExport {
canonical_path: target.clone(),
visibility: super::ExportVisibility::Public,
declaration_visibility: super::ExportVisibility::Public,
name: segment.into(),
target_module: Some(target.clone()),
span: None,
definition_id: None,
external_function: None,
kind: SymbolKind::Module,
inferred_type: None,
detail: Some(format!("module {target}")),
module_path: parent,
fields: Vec::new(),
});
}
parent = target;
}
}