Skip to main content

lisette_syntax/program/
resolution.rs

1use rustc_hash::FxHashMap as HashMap;
2
3use crate::ast::{Expression, Span};
4use crate::types::Symbol;
5use crate::types::Type;
6
7pub type ResolvedDefinitions = HashMap<Span, Symbol>;
8
9pub fn resolved_definition<'a>(
10    expression: &'a Expression,
11    definitions: &'a ResolvedDefinitions,
12) -> Option<&'a str> {
13    match expression.unwrap_parens() {
14        Expression::Identifier {
15            qualified: Some(definition),
16            ..
17        } => Some(definition),
18        Expression::DotAccess { span, .. } => definitions.get(span).map(Symbol::as_str),
19        _ => None,
20    }
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum ReceiverCoercion {
25    /// Insert `&` to convert `T` to `Ref<T>`
26    AutoAddress,
27    /// Insert `*` to convert `Ref<T>` to `T`
28    AutoDeref,
29}
30
31/// What a dot access resolved to during type checking.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum DotAccessKind {
34    /// Named struct field access
35    StructField { is_exported: bool },
36    /// Tuple struct field access (e.g., `point.0` on `struct Point(int, int)`).
37    /// `is_newtype` is true when the struct has exactly 1 field and no generics,
38    /// meaning access should emit a type cast rather than `.F0`.
39    TupleStructField { is_newtype: bool },
40    /// Tuple element access (e.g., `t.0`, `t.1`)
41    TupleElement,
42    /// Module member access (e.g., `mod.func`)
43    ModuleMember,
44    /// ADT enum variant constructor (e.g., `makeColorRed[T]()`)
45    EnumVariant,
46    /// Instance method (has `self` receiver)
47    InstanceMethod { is_exported: bool },
48    /// Instance method used as a first-class value (not called).
49    /// E.g., `Point.area` used as a callback. The emitter needs to know
50    /// whether the receiver is a pointer to emit Go method expression syntax.
51    InstanceMethodValue {
52        is_exported: bool,
53        is_pointer_receiver: bool,
54    },
55    /// Static method (no `self` receiver)
56    StaticMethod { is_exported: bool },
57}
58
59/// What kind of native built-in type (Slice, Map, Channel, etc.) a call targets.
60/// Defined here so semantics can classify calls without depending on
61/// emit-specific types. The emitter maps this to its internal `NativeGoType`.
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub enum NativeTypeKind {
64    Slice,
65    EnumeratedSlice,
66    Map,
67    Channel,
68    Sender,
69    Receiver,
70    String,
71    Array,
72}
73
74impl NativeTypeKind {
75    pub fn from_type(ty: &Type) -> Option<Self> {
76        let resolved = ty.strip_refs();
77        // Skip module namespaces and Go-imported types: their leaf name can
78        // collide with a native type (e.g. `Slice`), but they are not native.
79        if resolved.as_import_namespace().is_some() {
80            return None;
81        }
82        if let Type::Nominal { ref id, .. } = resolved
83            && id.as_str().starts_with("go:")
84        {
85            return None;
86        }
87        let name = resolved.get_name()?;
88        Self::from_name(name)
89    }
90
91    pub fn from_name(name: &str) -> Option<Self> {
92        match name {
93            "Slice" => Some(Self::Slice),
94            "EnumeratedSlice" => Some(Self::EnumeratedSlice),
95            "Map" => Some(Self::Map),
96            "Channel" => Some(Self::Channel),
97            "Sender" => Some(Self::Sender),
98            "Receiver" => Some(Self::Receiver),
99            "string" => Some(Self::String),
100            "Array" => Some(Self::Array),
101            _ => None,
102        }
103    }
104}
105
106/// What a call expression resolved to during type checking.
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108pub enum CallKind {
109    /// Regular function or method call
110    Regular,
111    /// Tuple struct constructor (e.g., `Point(1, 2)`)
112    TupleStructConstructor,
113    /// Type assertion (`assert_type`)
114    AssertType,
115    /// UFCS method call: `receiver.method()` where method is a free function
116    UfcsMethod,
117    /// Native type constructor (e.g., `Channel.new`, `Map.new`, `Slice.new`)
118    NativeConstructor(NativeTypeKind),
119    /// Native type instance method via dot access (e.g., `slice.append(x)`)
120    NativeMethod(NativeTypeKind),
121    /// Native type method via identifier (e.g., `Slice.contains(s, x)`)
122    NativeMethodIdentifier(NativeTypeKind),
123    /// Receiver method in UFCS syntax: `Type.method(receiver, args)`
124    ReceiverMethodUfcs { is_public: bool },
125}