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
extern crate object;

use object::Object;

mod parser;
use parser::{ parse, LittleEndian, BigEndian };

use std::collections::BTreeMap;
use std::fmt;
use std::fmt::Write;

pub struct Symbols {
    pub functions: BTreeMap<String, Function>
}

#[derive(Clone)]
pub struct Function {
    pub name: Option<String>,
    pub typed: Typed,
    pub parameters: Parameters
}

#[derive(Clone)]
pub struct Parameter {
    pub name: Option<String>,
    pub typed: Typed
}
pub type Parameters = Vec<Parameter>;
pub type Member = Parameter;
pub type Members = Vec<Member>;

#[derive(Clone)]
pub struct Typed {
    pub name: String,
    pub value: TypedValue,
    pub modifiers: Modifiers
}

#[derive(Clone)]
#[derive(Debug)]
pub enum TypedValue {
    Base,
    Enum,
    Typedef(Box<Typed>),
    Function(Box<Function>),
    Struct(Members),
    Union(Members),
    Array(Box<Typed>, u16),
    Circular
}

#[derive(Clone)]
pub enum Modifier {
    Pointer,
    Reference,
    Const,
    Volatile,
    Restrict
}
pub type Modifiers = Vec<Modifier>;

impl Symbols {
    pub fn from(file: object::File) -> Symbols {
        if file.is_little_endian() {
            parse::<LittleEndian>(file)
        } else {
            parse::<BigEndian>(file)
        }
    }

    fn new() -> Symbols {
        Symbols {
            functions: BTreeMap::new()
        }
    }
}

impl fmt::Debug for Function {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let typeds = self.parameters.iter().fold(format!("\n{:?}", self.typed), |mut s, p| {
            let _ = write!(s, "\n{:?}", p);
            s
        });
        write!(f, "{}{}", self, typeds)
    }
}

impl fmt::Display for Function {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Vec cannot impl fmt::Display, so it is done here
        let parameters = self.parameters.iter().fold(String::new(), |mut s, p| {
            let _ = if s.is_empty() {
                write!(s, "{}", p)
            } else {
                write!(s, ", {}", p)
            };
            s
        });

        match self.name {
            Some(ref name) => write!(f, "{} {}({})", name, self.typed, parameters),
            None => write!(f, "{}({})", self.typed, parameters)
        }
    }
}

impl fmt::Debug for Parameter {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let name = match self.name {
            Some(ref name) => format!("name: {}\t", name),
            None => String::new()
        };
        write!(f, "{}{:?}", name, self.typed)
    }
}

impl fmt::Display for Parameter {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.typed.value {
            TypedValue::Function(ref function) => {
                let mut function_ptr = (*function).clone();
                function_ptr.name = Some(match self.name {
                    Some(ref name) => format!("(*{})", name),
                    None => String::from("(*)")
                });
                write!(f, "{}", function_ptr)
            },
            _ => {
                match self.name {
                    Some(ref name) => write!(f, "{} {}", self.typed, name),
                    None => write!(f, "{}", self.typed)
                }
            }
        }
    }
}

impl fmt::Debug for Typed {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {:#?}", self, self.value)
    }
}

impl fmt::Display for Typed {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let specifier = self.modifiers.iter().fold(self.name.clone(), |s, m| {
            match m {
                &Modifier::Pointer => s + "*",
                &Modifier::Reference => s + "&",
                &Modifier::Const => s + " const",
                &Modifier::Volatile => s + " volatile",
                &Modifier::Restrict => s + " restrict"
            }
        });
        write!(f, "{}", specifier)
    }
}