tin/ir/component/
symbol.rs

1use std::fmt;
2
3use specs::Component;
4use specs::VecStorage;
5
6#[derive(Component, Clone, Debug, VisitEntities, VisitEntitiesMut)]
7#[storage(VecStorage)]
8pub struct Symbol {
9    public: bool,
10    parts: Vec<Part>,
11}
12
13#[derive(Clone, Debug, VisitEntities, VisitEntitiesMut)]
14pub enum Part {
15    Named(String),
16    #[allow(unused)]
17    Unnamed(u64),
18}
19
20impl Symbol {
21    pub fn new(parts: Vec<Part>) -> Self {
22        let public = false;
23        Symbol { public, parts }
24    }
25
26    pub fn into_public(self) -> Self {
27        let public = true;
28        let parts = self.parts;
29        Symbol { public, parts }
30    }
31
32    pub fn is_empty(&self) -> bool {
33        self.parts.is_empty()
34    }
35
36    pub fn is_public(&self) -> bool {
37        self.public
38    }
39
40    pub fn is_top_level(&self) -> bool {
41        self.parts.len() == 1
42    }
43}
44
45impl fmt::Display for Symbol {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        if self.public {
48            f.write_str("public:")?;
49        }
50
51        let mut needs_sep = false;
52
53        for part in &self.parts {
54            if needs_sep {
55                f.write_str(".")?;
56            }
57            part.fmt(f)?;
58            needs_sep = true;
59        }
60
61        Ok(())
62    }
63}
64
65impl fmt::Display for Part {
66    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67        match *self {
68            Part::Named(ref name) => name.fmt(f),
69            Part::Unnamed(ref id) => id.fmt(f),
70        }
71    }
72}