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
use galvan_ast::{FnDecl, Ident, MainDecl, SegmentedAsts, ToplevelItem, TypeDecl, TypeIdent};
use std::collections::HashMap;
use thiserror::Error;

#[derive(Debug, Default)]
pub struct LookupContext<'a> {
    /// Types are resolved by their name
    pub types: HashMap<TypeIdent, &'a ToplevelItem<TypeDecl>>,
    /// Functions are resolved by their name and - if present - named arguments and their receiver type
    ///
    /// `fn foo(a: i32, b: i32) -> i32` is identified as `foo`
    /// `fn foo(bar a: i32, b: i32) -> i32` is identified as `foo:bar`
    /// `fn foo(self: i32, b: i32) -> i32` is identified as `i32::foo`
    pub functions: HashMap<FunctionId, &'a ToplevelItem<FnDecl>>,
    // TODO: Nested contexts for resolving names from imported modules
    // pub imports: HashMap<String, LookupContext<'a>>,
    pub main: Option<&'a ToplevelItem<MainDecl>>,
}

// TODO: Include spans in errors
#[derive(Debug, Error)]
pub enum LookupError {
    #[error("Type not found")]
    TypeNotFound,
    #[error("Function not found")]
    FunctionNotFound,
    #[error("Duplicate type")]
    DuplicateType(TypeIdent),
    #[error("Duplicate function")]
    DuplicateFunction,
}

impl<'a> LookupContext<'a> {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn add_from(&mut self, asts: &'a SegmentedAsts) -> Result<(), LookupError> {
        for func in &asts.functions {
            let func_id = FunctionId::new(None, &func.signature.identifier, &[]);
            if self.functions.insert(func_id, func).is_some() {
                return Err(LookupError::DuplicateFunction);
            }
        }

        for type_decl in &asts.types {
            if self
                .types
                .insert(type_decl.ident().clone(), type_decl)
                .is_some()
            {
                return Err(LookupError::DuplicateType(type_decl.ident().clone()));
            }
        }

        Ok(())
    }

    pub fn with(mut self, asts: &'a SegmentedAsts) -> Result<Self, LookupError> {
        self.add_from(asts)?;
        Ok(self)
    }
}

impl LookupContext<'_> {
    pub fn resolve_type(&self, name: &TypeIdent) -> Option<&ToplevelItem<TypeDecl>> {
        self.types.get(&name).copied()
    }

    pub fn resolve_function(
        &self,
        receiver: Option<&TypeIdent>,
        name: &Ident,
        labels: &[&str],
    ) -> Option<&ToplevelItem<FnDecl>> {
        let func_id = FunctionId::new(receiver, name, labels);
        self.functions.get(&func_id).copied()
    }
}

#[derive(Debug, Hash, PartialEq, Eq)]
pub struct FunctionId(Box<str>);

impl FunctionId {
    fn new(receiver: Option<&TypeIdent>, fn_ident: &Ident, labels: &[&str]) -> Self {
        let mut id = String::new();
        if let Some(receiver) = receiver {
            id.push_str(receiver.as_str());
            id.push_str("::");
        }
        id.push_str(fn_ident.as_str());
        if !labels.is_empty() {
            id.push(':');
            id.push_str(&labels.join(":"));
        }

        Self(id.into())
    }
}