lisette-emit 0.2.6

Little language inspired by Rust that compiles to Go
Documentation
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use syntax::program::DefinitionBody;

use crate::Emitter;
use crate::bindings::BindingValue;
use crate::expressions::context::ExpressionContext;
use crate::names::generics::extract_type_mapping;
use crate::names::go_name;
use syntax::types::{Type, unqualified_name};

pub(crate) enum IdentifierKind {
    /// `Unit` used as expression value → `struct{}{}`
    UnitValue,
    /// Value enum variant (e.g., `Color.Red`) → module-qualified constant
    ValueEnumVariant { go_constant: String },
    /// Public function needing Go capitalization
    PublicFunction { capitalized: String },
    /// Enum variant unit constructor → `MakeName[Types]()`
    UnitConstructor { name: String, type_args: String },
    /// Enum variant constructor as function value → `MakeName[Types]`
    ConstructorFunction { name: String, type_args: String },
    /// Regular identifier (may need static method capitalization or cross-module resolution)
    Regular { name: String },
}

impl Emitter<'_> {
    pub(crate) fn emit_identifier(
        &mut self,
        value: &str,
        ty: &Type,
        ctx: ExpressionContext<'_>,
    ) -> String {
        if let Some(BindingValue::InlineExpr(expr)) = self.scope.resolve_identifier_binding(value) {
            return expr.as_str().to_string();
        }
        match self.classify_identifier(value, ty, ctx) {
            IdentifierKind::UnitValue => "struct{}{}".to_string(),
            IdentifierKind::ValueEnumVariant { go_constant } => go_constant,
            IdentifierKind::PublicFunction { capitalized } => capitalized,
            IdentifierKind::UnitConstructor { name, type_args } => {
                format!("{}{}()", self.resolve_go_name(&name), type_args)
            }
            IdentifierKind::ConstructorFunction { name, type_args } => {
                format!("{}{}", self.resolve_go_name(&name), type_args)
            }
            IdentifierKind::Regular { name } => {
                if let Some(expression) = self.try_emit_method_expression(&name, ty) {
                    return expression;
                }
                let resolved = self.capitalize_static_method_if_public(&name);
                let go_name = self.resolve_go_name(&resolved);
                if !ctx.is_callee()
                    && let Some(type_args) = self.format_generic_value_type_args(&name, ty)
                {
                    return format!("{}{}", go_name, type_args);
                }
                go_name
            }
        }
    }

    fn classify_identifier(
        &mut self,
        value: &str,
        ty: &Type,
        ctx: ExpressionContext<'_>,
    ) -> IdentifierKind {
        if value == "Unit" && ty.is_unit() {
            return IdentifierKind::UnitValue;
        }

        let name = self
            .scope
            .resolve_binding_go_name(value)
            .unwrap_or(value)
            .to_string();

        if let Some(go_constant) = self.try_classify_value_enum_variant(&name, ty) {
            return IdentifierKind::ValueEnumVariant { go_constant };
        }

        if let Some(capitalized) = self.try_capitalize_public_function(&name, ty) {
            return IdentifierKind::PublicFunction { capitalized };
        }

        let mut make_fn = self.facts.make_function_name(&name);

        if make_fn.is_none() {
            let enum_id = match ty {
                Type::Function { return_type, .. } => {
                    if let Type::Nominal { id, .. } = return_type.as_ref() {
                        Some(id.as_str())
                    } else {
                        None
                    }
                }
                Type::Nominal { id, .. } => Some(id.as_str()),
                _ => None,
            };

            if let Some(id) = enum_id {
                let enum_name = unqualified_name(id);
                let qualified = format!("{}.{}", enum_name, value);
                make_fn = self.facts.make_function_name(&qualified);
            }
        }

        if let Some(make_fn_value) = make_fn {
            let name = make_fn_value.to_string();

            match ty {
                Type::Nominal { params, .. } => {
                    let type_args = ctx
                        .expected_slot_type()
                        .and_then(|t| self.prelude_container_type_args(t))
                        .unwrap_or_else(|| self.format_type_args(params));
                    return IdentifierKind::UnitConstructor { name, type_args };
                }

                Type::Function {
                    params: fn_params,
                    return_type,
                    ..
                } => {
                    if let Type::Nominal {
                        params: ret_params, ..
                    } = return_type.as_ref()
                    {
                        let type_args = self.constructor_fn_type_args(fn_params, ret_params, ctx);
                        return IdentifierKind::ConstructorFunction { name, type_args };
                    }
                }

                _ => unreachable!("make_fn set for unexpected type: {:?}", ty),
            }
        }

        let resolved = make_fn.map(str::to_string).unwrap_or(name);
        IdentifierKind::Regular { name: resolved }
    }

    /// Type args for a constructor function reference (e.g. `MakeFoo[T]` used as a value).
    /// Skips type args when the callee position already supplies them or when they can be
    /// inferred from the parameter types.
    fn constructor_fn_type_args(
        &mut self,
        fn_params: &[Type],
        ret_params: &[Type],
        ctx: ExpressionContext<'_>,
    ) -> String {
        let needs_type_args = !ctx.is_callee()
            || ret_params.len() > fn_params.len()
            || !ret_params
                .iter()
                .all(|rp| fn_params.iter().any(|fp| fp.contains_type(rp)))
            || fn_params
                .iter()
                .any(|t| self.needs_explicit_args_for_go_inference(t));
        if needs_type_args {
            self.format_type_args(ret_params)
        } else {
            String::new()
        }
    }

    /// The identifier's type is already instantiated (Forall stripped by the type checker).
    /// We look up the definition to find the generic signature, then extract concrete
    /// types by matching the generic body against the instantiated type.
    fn format_generic_value_type_args(
        &mut self,
        name: &str,
        instantiated_ty: &Type,
    ) -> Option<String> {
        let qualified_name = self.facts.qualified_current(name);
        let definition_ty = self
            .facts
            .definition(qualified_name.as_str())
            .or_else(|| {
                let prelude_name = format!("{}.{}", go_name::PRELUDE_MODULE, name);
                self.facts.definition(prelude_name.as_str())
            })?
            .ty();

        let Type::Forall { vars, body } = definition_ty else {
            return None;
        };

        let mut mapping = rustc_hash::FxHashMap::default();
        extract_type_mapping(body, instantiated_ty, &mut mapping);

        let args: Vec<String> = vars
            .iter()
            .filter_map(|var| {
                let concrete = mapping.get(var.as_str())?;
                Some(self.go_type_as_string(concrete))
            })
            .collect();

        if args.len() != vars.len()
            || args.is_empty()
            || args.iter().any(|a| a.contains("interface{}"))
        {
            return None;
        }

        Some(format!("[{}]", args.join(", ")))
    }

    /// Like `format_generic_value_type_args` but takes a pre-qualified definition name
    /// instead of constructing one from the current module.
    pub(crate) fn format_cross_module_type_args(
        &mut self,
        qualified_name: &str,
        instantiated_ty: &Type,
    ) -> Option<String> {
        let definition_ty = self.facts.definition(qualified_name)?.ty().clone();

        let Type::Forall { vars, body } = &definition_ty else {
            return None;
        };

        let mut mapping = rustc_hash::FxHashMap::default();
        extract_type_mapping(body, instantiated_ty, &mut mapping);

        let args: Vec<String> = vars
            .iter()
            .filter_map(|var| {
                let concrete = mapping.get(var.as_str())?;
                Some(self.go_type_as_string(concrete))
            })
            .collect();

        if args.len() != vars.len()
            || args.is_empty()
            || args.iter().any(|a| a.contains("interface{}"))
        {
            return None;
        }

        Some(format!("[{}]", args.join(", ")))
    }

    /// Check if a dotted name like "Type.method" refers to a receiver method,
    /// and if so return Go method expression syntax instead of free function name.
    ///
    /// Uses the identifier's type (from the AST) to determine whether the first
    /// parameter is `self` — this is reliable because the type checker includes
    /// the receiver as the first param for instance methods but not static ones.
    fn try_emit_method_expression(&mut self, name: &str, id_ty: &Type) -> Option<String> {
        let (type_part, method_part) = name.split_once('.')?;

        if method_part.contains('.') {
            return None;
        }

        let fn_params = match id_ty {
            Type::Function { params, .. } => params,
            Type::Forall { body, .. } => match body.as_ref() {
                Type::Function { params, .. } => params,
                _ => return None,
            },
            _ => return None,
        };

        let real_type_part = self
            .resolve_alias_type_name(type_part)
            .unwrap_or_else(|| type_part.to_string());
        let qualified_name = self.facts.qualified_current(&real_type_part);
        let first = fn_params.first()?;
        let stripped = first.strip_refs();
        let is_self =
            matches!(stripped, Type::Nominal { ref id, .. } if id.as_str() == qualified_name);
        if !is_self {
            return None;
        }
        let type_part = &real_type_part;

        let is_pointer = first.is_ref();

        if self.facts.is_ufcs_method(&qualified_name, method_part) {
            return None;
        }

        let method_key = self.facts.qualified_current_member(type_part, method_part);
        let should_export = self
            .facts
            .definition(method_key.as_str())
            .map(|d| d.visibility().is_public())
            .unwrap_or(false)
            || self.method_needs_export(method_part);
        let go_method = if should_export {
            go_name::snake_to_camel(method_part)
        } else {
            go_name::escape_keyword(method_part).into_owned()
        };

        let type_args = if let Type::Nominal { ref params, .. } = stripped {
            if params.is_empty() {
                String::new()
            } else {
                self.format_type_args(params)
            }
        } else {
            String::new()
        };

        if is_pointer {
            Some(format!("(*{}{}).{}", type_part, type_args, go_method))
        } else {
            Some(format!("{}{}.{}", type_part, type_args, go_method))
        }
    }

    /// Attempt to resolve a cross-module static method call name using qualify_method.
    /// Returns Some if name matches pattern "module.Type.method", None to fall through.
    pub(crate) fn try_resolve_cross_module_static_method(&mut self, name: &str) -> Option<String> {
        if !name.contains('.') {
            return None;
        }

        let last_dot = name.rfind('.')?;
        let method_name = &name[last_dot + 1..];
        let type_and_module = &name[..last_dot];

        let type_name = if let Some(dot_position) = type_and_module.rfind('.') {
            &type_and_module[dot_position + 1..]
        } else if let Some(slash_position) = type_and_module.rfind('/') {
            &type_and_module[slash_position + 1..]
        } else {
            return None;
        };

        let module_name = if let Some(dot_position) = type_and_module.rfind('.') {
            type_and_module[..dot_position].to_string()
        } else if let Some(slash_position) = type_and_module.rfind('/') {
            type_and_module[..slash_position].to_string()
        } else {
            return None;
        };

        if self.facts.is_current_module(&module_name) {
            return None;
        }

        let type_id = format!("{}.{}", module_name, type_name);

        let method_key = format!("{}.{}", type_id, method_name);
        let is_public = self
            .facts
            .definition(method_key.as_str())
            .map(|d| d.visibility().is_public())
            .unwrap_or(true)
            || self.method_needs_export(method_name);

        Some(self.qualify_method_call(&type_id, method_name, is_public))
    }

    fn try_classify_value_enum_variant(&mut self, name: &str, ty: &Type) -> Option<String> {
        if !name.contains('.') {
            return None;
        }

        let Type::Nominal { id: enum_id, .. } = ty else {
            return None;
        };

        let definition = self.facts.definition(enum_id.as_str())?;
        if !matches!(definition.body, DefinitionBody::ValueEnum { .. }) {
            return None;
        }

        let variant_name = go_name::unqualified_name(name);
        let module = self.facts.module_for_qualified_name(enum_id.as_str())?;
        let qualifier = self.require_module_import(module);

        Some(format!("{}.{}", qualifier, variant_name))
    }

    /// Check if an identifier refers to a public function in the current module.
    /// If so, return its capitalized Go name.
    fn try_capitalize_public_function(&self, name: &str, ty: &Type) -> Option<String> {
        let is_function = matches!(ty, Type::Function { .. })
            || matches!(ty, Type::Forall { body, .. } if matches!(body.as_ref(), Type::Function { .. }));
        if !is_function {
            return None;
        }

        if self.scope.resolve_identifier_binding(name).is_some() {
            return None;
        }

        if name.contains('.') {
            return None;
        }

        let qualified_name = self.facts.qualified_current(name);
        let definition = self.facts.definition(qualified_name.as_str())?;

        if !definition.visibility().is_public() {
            return None;
        }

        Some(go_name::snake_to_camel(name))
    }
}