lisette_syntax/program/
go_method_policy.rs1use super::{Definition, DefinitionBody, InterfaceRequirement, interface_requirements};
2use super::{is_go_import, is_prelude_symbol};
3use crate::go_names::snake_to_camel;
4use crate::types::{Symbol, Type, TypeKind, unqualified_name};
5use std::collections::BTreeSet;
6
7#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
8pub struct GoMethodPolicy {
9 pub exported_method_names: BTreeSet<String>,
10 pub tagged_method_names: BTreeSet<String>,
11}
12
13impl GoMethodPolicy {
14 pub fn exports_method(&self, method_name: &str) -> bool {
15 self.exported_method_names.contains(method_name)
16 || matches!(method_name, "string" | "goString" | "error")
17 }
18
19 pub fn for_definitions<'a>(
20 &self,
21 definitions: impl Iterator<Item = (&'a Symbol, &'a Definition)>,
22 ) -> Self {
23 let mut selectors: Vec<&str> = Vec::new();
24 for (name, definition) in definitions {
25 if let Some(methods) = definition.methods() {
26 selectors.extend(methods.values().map(|method| method.source_name.as_str()));
27 }
28 if matches!(definition.body, DefinitionBody::Value { .. })
29 && name.bytes().filter(|byte| *byte == b'.').nth(1).is_some()
30 {
31 selectors.push(unqualified_name(name));
32 }
33 }
34 selectors.sort_unstable();
35 selectors.dedup();
36 let tagged_method_names = if self.tagged_method_names.is_empty() {
37 BTreeSet::new()
38 } else {
39 selectors
40 .iter()
41 .map(|name| snake_to_camel(name))
42 .filter(|name| self.tagged_method_names.contains(name))
43 .collect()
44 };
45 Self {
46 exported_method_names: selectors
47 .iter()
48 .filter(|name| self.exported_method_names.contains(**name))
49 .map(|name| (*name).to_string())
50 .collect(),
51 tagged_method_names,
52 }
53 }
54
55 pub fn compute<'a>(definitions: impl Iterator<Item = (&'a Symbol, &'a Definition)>) -> Self {
56 let mut policy = Self::default();
57 for (key, definition) in definitions {
58 policy.register_exported_methods(key, definition);
59 policy.register_tagged_interface_methods(key, definition);
60 }
61 policy
62 }
63
64 fn register_tagged_interface_methods(&mut self, key: &Symbol, definition: &Definition) {
65 if is_go_import(key) {
66 return;
67 }
68 let DefinitionBody::Interface {
69 definition: interface_definition,
70 } = &definition.body
71 else {
72 return;
73 };
74 for method in interface_definition.methods.values() {
75 if interface_method_is_tagged(&method.ty.body) {
76 self.tagged_method_names
77 .insert(snake_to_camel(&method.source_name));
78 }
79 }
80 }
81
82 fn register_exported_methods(&mut self, key: &Symbol, definition: &Definition) {
83 let is_user_definition = !is_go_import(key) && !is_prelude_symbol(key);
84 match &definition.body {
85 DefinitionBody::Interface {
86 definition: interface_definition,
87 ..
88 } if definition.visibility.is_public() => {
89 for method_name in interface_definition.methods.keys() {
90 self.exported_method_names.insert(method_name.to_string());
91 }
92 }
93 DefinitionBody::Value { .. }
94 if definition.visibility.is_public()
95 && is_user_definition
96 && key.split('.').nth(2).is_some() =>
97 {
98 let method_name = unqualified_name(key);
99 self.exported_method_names.insert(method_name.to_string());
100 }
101 _ => {}
102 }
103
104 if is_user_definition && let Some(methods) = definition.methods() {
105 for method in methods
106 .values()
107 .filter(|method| method.visibility.is_public())
108 {
109 self.exported_method_names
110 .insert(method.source_name.to_string());
111 }
112 }
113
114 if definition.visibility.is_public() && definition.is_display() {
115 self.exported_method_names.insert("to_string".to_string());
116 }
117 }
118}
119
120pub fn interface_method_is_tagged(method_ty: &Type) -> bool {
121 let Some(return_ty) = method_ty.get_function_ret() else {
122 return false;
123 };
124 let peeled = return_ty.without_spelling();
125 match peeled.kind() {
126 TypeKind::Parameter(_) => true,
127 TypeKind::Nominal { params, .. } if peeled.is_option() || peeled.is_result() => {
128 params.iter().any(Type::contains_type_parameter)
129 }
130 _ => peeled.is_partial(),
131 }
132}
133
134pub fn requirement_uses_tagged_counterpart(requirement: &InterfaceRequirement) -> bool {
135 !is_go_import(&requirement.declaring_interface)
136 && interface_method_is_tagged(&requirement.method.ty.body)
137}
138
139pub fn supplying_requirement<'a>(
140 suppliers: &[Type],
141 name: &str,
142 wanted: Option<&InterfaceRequirement>,
143 lookup: impl Fn(&str) -> Option<&'a Definition> + Copy,
144) -> Option<InterfaceRequirement> {
145 let mut candidates: Vec<InterfaceRequirement> = suppliers
146 .iter()
147 .flat_map(|supplier| interface_requirements(supplier, lookup))
148 .filter(|candidate| candidate.name == name)
149 .collect();
150 let index = wanted
151 .and_then(|wanted| {
152 let tagged = requirement_uses_tagged_counterpart(wanted);
153 candidates
154 .iter()
155 .position(|candidate| requirement_uses_tagged_counterpart(candidate) == tagged)
156 })
157 .unwrap_or(0);
158 (index < candidates.len()).then(|| candidates.swap_remove(index))
159}