lisette-emit 0.2.5

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
405
406
407
408
409
use syntax::ast::{Expression, StructKind, UnaryOperator};
use syntax::program::{
    Definition, DefinitionBody, DotAccessKind as SemanticDotKind, ReceiverCoercion,
};
use syntax::types::Type;

use crate::Emitter;
use crate::expressions::context::ExpressionContext;
use crate::go_name;
use crate::types::coercion::{Coercion, CoercionDirection};

impl Emitter<'_> {
    pub(crate) fn emit_dot_access(
        &mut self,
        output: &mut String,
        dot_access: &Expression,
        ctx: ExpressionContext<'_>,
    ) -> String {
        let Expression::DotAccess {
            expression,
            member,
            ty: result_ty,
            dot_access_kind,
            receiver_coercion,
            ..
        } = dot_access
        else {
            unreachable!("emit_dot_access requires a DotAccess expression");
        };
        let dot_access_kind = *dot_access_kind;
        let receiver_coercion = *receiver_coercion;

        if let Some(s) =
            self.try_emit_pre_receiver_dot(expression, member, result_ty, dot_access_kind, ctx)
        {
            return s;
        }

        let expression_string =
            self.emit_coerced_expression(output, expression, receiver_coercion, ctx);
        let expression_ty = expression.get_type();

        if let Some(module) = expression_ty.as_import_namespace() {
            self.require_module_import(module);
        }

        if let Some(s) = self.try_emit_tuple_member_dot(
            &expression_string,
            &expression_ty,
            member,
            dot_access_kind,
        ) {
            return s;
        }

        let is_exported =
            self.resolve_is_exported(expression, &expression_ty, member, dot_access_kind);
        let field = go_field_name(&expression_ty, member, is_exported);

        if let Some(s) = self.try_emit_nullable_field_access(
            output,
            &expression_string,
            &field,
            &expression_ty,
            result_ty,
        ) {
            return s;
        }

        let result = format!("{}.{}", expression_string, field);
        self.append_cross_module_type_args(result, &expression_ty, member, result_ty, ctx)
    }

    /// Phase 1 dispatch: the semantic kind may resolve without needing the
    /// receiver emitted first (value-enum variant, enum constructor, static
    /// method, instance-method value). `ModuleMember` and unresolved kinds
    /// may still resolve as an enum variant or static method under a cross-
    /// module/alias rename, so both helpers are tried in order.
    fn try_emit_pre_receiver_dot(
        &mut self,
        expression: &Expression,
        member: &str,
        result_ty: &Type,
        dot_access_kind: Option<SemanticDotKind>,
        ctx: ExpressionContext<'_>,
    ) -> Option<String> {
        match dot_access_kind {
            Some(SemanticDotKind::ValueEnumVariant) => {
                self.emit_value_enum_variant(expression, member)
            }
            Some(SemanticDotKind::EnumVariant) => {
                self.emit_enum_variant_dot(expression, member, result_ty)
            }
            Some(SemanticDotKind::StaticMethod { .. }) => {
                self.emit_static_method_dot(expression, member, result_ty, ctx)
            }
            Some(SemanticDotKind::InstanceMethodValue {
                is_exported,
                is_pointer_receiver,
            }) => self.emit_instance_method_value_dot(
                expression,
                member,
                result_ty,
                is_exported,
                is_pointer_receiver,
            ),
            Some(SemanticDotKind::ModuleMember) | None => self
                .emit_enum_variant_dot(expression, member, result_ty)
                .or_else(|| self.emit_static_method_dot(expression, member, result_ty, ctx)),
            _ => None,
        }
    }

    /// Tuple-shape members: plain tuple slots emit as `.F{index}` (or the
    /// `TUPLE_FIELDS` name); tuple-struct slots additionally try a newtype
    /// cast when the struct has a single field and no generics.
    fn try_emit_tuple_member_dot(
        &mut self,
        expression_string: &str,
        expression_ty: &Type,
        member: &str,
        dot_access_kind: Option<SemanticDotKind>,
    ) -> Option<String> {
        let Ok(index) = member.parse::<usize>() else {
            return None;
        };
        match dot_access_kind {
            Some(SemanticDotKind::TupleElement) => {
                let field = syntax::parse::TUPLE_FIELDS
                    .get(index)
                    .expect("oversize tuple arity");
                Some(format!("{}.{}", expression_string, field))
            }
            Some(SemanticDotKind::TupleStructField { is_newtype }) => {
                if is_newtype
                    && let Some(cast) = self.try_emit_newtype_cast(expression_ty, expression_string)
                {
                    return Some(cast);
                }
                Some(format!("{}.F{}", expression_string, index))
            }
            _ => None,
        }
    }

    /// Decide whether the Go member name needs exporting (capitalization).
    /// Semantic `is_exported` covers cross-module + public visibility; the
    /// emit-side checks additionally cover Go-specific concerns like
    /// `#[json]`-tagged fields and interface-method capitalization.
    fn resolve_is_exported(
        &self,
        expression: &Expression,
        expression_ty: &Type,
        member: &str,
        dot_access_kind: Option<SemanticDotKind>,
    ) -> bool {
        match dot_access_kind {
            Some(SemanticDotKind::StructField { is_exported }) => {
                is_exported || self.field_is_public(expression_ty, member)
            }
            Some(SemanticDotKind::InstanceMethod { is_exported }) => {
                is_exported || self.method_needs_export(member)
            }
            _ => {
                self.compute_is_exported_context(expression, expression_ty)
                    || self.field_is_public(expression_ty, member)
                    || (!self.has_field(expression_ty, member) && self.method_needs_export(member))
            }
        }
    }

    /// Accessing a nullable field on a Go-imported type: capture the raw
    /// access into a temp and wrap in the Some/None nullable shape expected
    /// downstream. Returns `None` when no wrapping is needed.
    fn try_emit_nullable_field_access(
        &mut self,
        output: &mut String,
        expression_string: &str,
        field: &str,
        expression_ty: &Type,
        result_ty: &Type,
    ) -> Option<String> {
        if !Self::is_go_imported_type(expression_ty) || !self.is_go_nullable(result_ty) {
            return None;
        }
        let raw_access = format!("{}.{}", expression_string, field);
        let raw_var = self.hoist_tmp_value(output, "raw", &raw_access);
        let coercion = Coercion::resolve(
            self,
            result_ty,
            result_ty,
            CoercionDirection::FromGoBoundary,
        );
        Some(coercion.apply(self, output, raw_var))
    }

    /// When accessing a cross-module generic member by value (not as a callee),
    /// look up the instantiation's type args and append them to the expression.
    /// Callee-position accesses skip this because the call site re-instantiates.
    fn append_cross_module_type_args(
        &mut self,
        base_access: String,
        expression_ty: &Type,
        member: &str,
        result_ty: &Type,
        ctx: ExpressionContext<'_>,
    ) -> String {
        if ctx.is_callee() {
            return base_access;
        }
        let Some(module) = expression_ty.as_import_namespace() else {
            return base_access;
        };
        let qualified = format!("{}.{}", module, member);
        match self.format_cross_module_type_args(&qualified, result_ty) {
            Some(type_args) => format!("{}{}", base_access, type_args),
            None => base_access,
        }
    }

    /// Emit a newtype cast like `MyType(inner)` for single-field tuple struct access.
    /// Returns None if the struct shape doesn't match (no single field, non-struct type).
    fn try_emit_newtype_cast(
        &mut self,
        expression_ty: &Type,
        expression_string: &str,
    ) -> Option<String> {
        let deref_ty = expression_ty.strip_refs();
        let Type::Nominal { id, .. } = &deref_ty else {
            return None;
        };
        let Some(Definition {
            body: DefinitionBody::Struct { fields, .. },
            ..
        }) = self.facts.definition(id.as_str())
        else {
            return None;
        };
        let field_ty = fields.first()?.ty.clone();
        let go_type = self.go_type_as_string(&field_ty);
        let operand = if expression_ty.is_ref() {
            format!("*{}", expression_string)
        } else {
            expression_string.to_string()
        };
        Some(if go_type.starts_with('*') {
            format!("({})({})", go_type, operand)
        } else {
            format!("{}({})", go_type, operand)
        })
    }

    /// Compute whether a dot access context requires exported (capitalized) Go names.
    /// Used as fallback when semantic DotAccessKind doesn't carry `is_exported`.
    fn compute_is_exported_context(&self, expression: &Expression, expression_ty: &Type) -> bool {
        let is_import_namespace_ident = matches!(
            expression,
            Expression::Identifier { ty, .. } if ty.as_import_namespace().is_some()
        );
        is_import_namespace_ident
            || self.is_from_prelude(expression_ty)
            || if let Type::Nominal { id, .. } = expression_ty.strip_refs() {
                id.split_once('.')
                    .is_some_and(|(m, _)| self.facts.is_foreign_module(m))
            } else {
                false
            }
    }

    /// Emit the base expression with receiver coercion applied.
    ///
    /// Handles explicit deref (`.*`), absorbed `Ref<T>` generics, and auto-address/auto-deref
    /// coercions. Returns the Go expression string ready for member access.
    fn emit_coerced_expression(
        &mut self,
        output: &mut String,
        expression: &Expression,
        coercion: Option<ReceiverCoercion>,
        ctx: ExpressionContext<'_>,
    ) -> String {
        let (expression_string, had_explicit_deref) = if let Expression::Unary {
            operator: UnaryOperator::Deref,
            expression: inner,
            ..
        } = expression
        {
            (self.emit_operand(output, inner, ctx), true)
        } else {
            (self.emit_operand(output, expression, ctx), false)
        };

        let is_absorbed_ref = self.is_absorbed_ref_generic(expression);

        match (coercion, had_explicit_deref) {
            _ if is_absorbed_ref => expression_string,
            (Some(ReceiverCoercion::AutoAddress), true) => expression_string,
            (Some(ReceiverCoercion::AutoAddress), false) => match expression.unwrap_parens() {
                Expression::Call { .. } => self.hoist_tmp_value(output, "ref", &expression_string),
                Expression::StructCall { .. } => format!("(&{})", expression_string),
                _ => expression_string,
            },
            (Some(ReceiverCoercion::AutoDeref), _) => expression_string,
            (None, true) => expression_string,
            (None, false) => expression_string,
        }
    }

    /// Check if expression has an absorbed `Ref<T>` generic (T already emitted as `*Concrete`).
    /// When true, suppress auto-deref coercion — the pointer is already the right type.
    fn is_absorbed_ref_generic(&self, expression: &Expression) -> bool {
        let check_expression = if let Expression::Unary {
            operator: UnaryOperator::Deref,
            expression: inner,
            ..
        } = expression
        {
            inner.as_ref()
        } else {
            expression
        };
        let expression_ty = check_expression.get_type();
        expression_ty.is_ref()
            && expression_ty.inner().is_some_and(|inner| {
                matches!(inner, Type::Parameter(name)
                    if self.function_state.is_absorbed_ref_generic(name.as_ref()))
            })
    }

    pub(crate) fn try_emit_tuple_struct_field_access(
        &mut self,
        expression_string: &str,
        expression_ty: &Type,
        index: usize,
    ) -> Option<String> {
        let deref_ty = expression_ty.strip_refs();
        let Type::Nominal { ref id, .. } = deref_ty else {
            return None;
        };

        let Some(Definition {
            body:
                DefinitionBody::Struct {
                    kind,
                    fields,
                    generics,
                    ..
                },
            ..
        }) = self.facts.definition(id.as_str())
        else {
            return None;
        };

        if *kind != StructKind::Tuple {
            return None;
        }

        if fields.len() == 1 && generics.is_empty() {
            let underlying_ty = self.go_type_as_string(&fields[0].ty);
            let expression = if expression_ty.is_ref() {
                format!("*{}", expression_string)
            } else {
                expression_string.to_string()
            };
            return Some(format!("{}({})", underlying_ty, expression));
        }

        Some(format!("{}.F{}", expression_string, index))
    }

    /// Whether the type resolves to a prelude-module declaration. Shared with
    /// the struct-call path, which also uses prelude-ness to decide field
    /// naming and type formatting.
    pub(super) fn is_from_prelude(&self, ty: &Type) -> bool {
        let Type::Nominal { id, .. } = ty.strip_refs() else {
            return false;
        };
        // Only return true if the type actually comes from the prelude module.
        // User-defined types with the same name should NOT be treated as prelude types.
        id.starts_with(go_name::PRELUDE_PREFIX)
    }
}

/// Pick the Go-side name for a struct field or method. Exported members on
/// prelude types follow snake_case → camelCase (matching the stdlib
/// convention); exported members elsewhere get first-letter capitalization;
/// non-exported members are escaped to avoid Go keywords.
fn go_field_name(expression_ty: &Type, member: &str, is_exported: bool) -> String {
    if expression_ty
        .as_import_namespace()
        .is_some_and(go_name::is_go_import)
    {
        return member.to_string();
    }

    let is_prelude_type = expression_ty
        .strip_refs()
        .get_qualified_id()
        .is_some_and(|id| id.starts_with(go_name::PRELUDE_PREFIX));

    if !is_exported {
        return go_name::escape_keyword(member).into_owned();
    }
    if is_prelude_type {
        go_name::snake_to_camel(member)
    } else {
        go_name::make_exported(member)
    }
}