spade_hir_lowering/
name_map.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4use spade_common::{id_tracker::ExprID, location_info::Loc, name::NameID};
5use spade_hir::{Expression, Pattern};
6use spade_mir::ValueName;
7
8#[derive(Clone, Serialize, Deserialize)]
9pub enum NameSource {
10    Name(Loc<NameID>),
11    Expr(Loc<ExprID>),
12}
13
14impl std::fmt::Display for NameSource {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        match self {
17            NameSource::Name(n) => write!(f, "{n}"),
18            NameSource::Expr(id) => write!(f, "e{}", id.0),
19        }
20    }
21}
22
23impl From<&Loc<NameID>> for NameSource {
24    fn from(n: &Loc<NameID>) -> Self {
25        NameSource::Name(n.clone())
26    }
27}
28
29impl From<&Loc<ExprID>> for NameSource {
30    fn from(e: &Loc<ExprID>) -> Self {
31        NameSource::Expr(*e)
32    }
33}
34
35impl From<&Loc<Expression>> for NameSource {
36    fn from(e: &Loc<Expression>) -> Self {
37        Self::from(&e.map_ref(|e| e.id))
38    }
39}
40
41impl From<&Loc<Pattern>> for NameSource {
42    fn from(e: &Loc<Pattern>) -> Self {
43        Self::from(&e.map_ref(|e| e.id))
44    }
45}
46
47#[derive(Clone, Serialize, Deserialize)]
48pub enum NamedValue {
49    /// This name corresponds to the primary expression of the specified name. I.e. the location
50    /// where the value is actually stored
51    Primary(NameSource),
52    /// This value is a secondary expression used in the calculation of the primary name. For
53    /// example, a boolean value or a pipelined version of a value. The associated string
54    /// is a description of the use
55    Secondary(NameSource, String),
56}
57
58impl std::fmt::Display for NamedValue {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        match self {
61            NamedValue::Primary(n) => write!(f, "{n}"),
62            NamedValue::Secondary(n, descr) => write!(f, "{n}({descr})"),
63        }
64    }
65}
66
67#[derive(Serialize, Deserialize, Clone)]
68pub struct NameSourceMap {
69    pub inner: HashMap<ValueName, NamedValue>,
70}
71
72impl Default for NameSourceMap {
73    fn default() -> Self {
74        Self::new()
75    }
76}
77
78impl NameSourceMap {
79    pub fn new() -> Self {
80        Self {
81            inner: HashMap::new(),
82        }
83    }
84
85    pub fn insert_primary(&mut self, name: &ValueName, unmangled: NameSource) {
86        self.inner
87            .insert(name.clone(), NamedValue::Primary(unmangled));
88    }
89    pub fn insert_secondary(
90        &mut self,
91        name: &ValueName,
92        unmangled: NameSource,
93        description: String,
94    ) {
95        self.inner
96            .insert(name.clone(), NamedValue::Secondary(unmangled, description));
97    }
98
99    pub fn merge(&mut self, other: NameSourceMap) {
100        for (k, v) in other.inner {
101            // NOTE: we previously had a check here for duplication, but that failed
102            // when monomorphising generic functions as we then had multiple
103            // copies of the same name in the output verilog.
104            //
105            // For the purpose of name mapping however, it does not matter what
106            // types were selected for the monomorphised item, so we can safely
107            // ignore duplicates.
108            self.inner.insert(k.clone(), v);
109        }
110    }
111}