~taiite/protodump

a1143a354c155cc02c022ebdbba8c4e35d58d80c — Hubert Hirtz 1 year, 5 months ago 7446255
public_types: Add types recursively
3 files changed, 49 insertions(+), 5 deletions(-)

M src/dwarf.rs
M src/main.rs
M src/types.rs
M src/dwarf.rs => src/dwarf.rs +32 -2
@@ 295,7 295,8 @@ where
    }

    pub fn public_types(&self) -> BTreeMap<String, types::TypePtr<R>> {
        self.subprograms
        let mut types: BTreeMap<_, _> = self
            .subprograms
            .iter()
            .flat_map(|(_, subprogram)| {
                let mut parameters = match &subprogram.parameters {


@@ 323,6 324,35 @@ where
                }
                parameters
            })
            .collect()
            .collect();
        loop {
            let new_types: BTreeMap<String, _> = types
                .iter()
                .flat_map(|(_, type_)| {
                    let tmp: Vec<_> = type_
                        .borrow()
                        .definition
                        .children()
                        .filter_map(|type_| {
                            let name = type_
                                .borrow()
                                .name
                                .as_ref()?
                                .to_string_lossy()
                                .unwrap()
                                .to_string();
                            Some((name, type_))
                        })
                        .collect();
                    tmp
                })
                .collect();
            let old_len = types.len();
            types.extend(new_types);
            if old_len == types.len() {
                break;
            }
        }
        types
    }
}

M src/main.rs => src/main.rs +0 -3
@@ 9,13 9,10 @@ use std::env;
use std::fmt;
use std::fs;
use std::process;
use types::TypePtr;
use yansi::Color;
use yansi::Paint;
use yansi::Style;

use crate::types::Definition;

mod dwarf;
mod types;


M src/types.rs => src/types.rs +17 -0
@@ 149,6 149,23 @@ impl<R> Definition<R> {
            encoding: gimli::DwAte(0),
        })
    }

    pub fn children<'a>(&'a self) -> impl Iterator<Item = TypePtr<R>> + 'a {
        let iter: Box<dyn Iterator<Item = InstancePtr<R>>> = match self {
            Self::Primitive(_) | Self::Enumeration { .. } => Box::new(std::iter::empty()),
            Self::Subroutine(subroutine) => Box::new(
                subroutine
                    .return_type
                    .clone()
                    .into_iter()
                    .chain(subroutine.parameters.iter().cloned()),
            ),
            Self::Structure { members } | Self::Union { members } => {
                Box::new(members.iter().map(|member| member.type_.clone()))
            }
        };
        iter.flat_map(|instance| instance.borrow().underlying.clone())
    }
}

impl<R> fmt::Display for Definition<R>