Skip to content

Commit

Permalink
Loads symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed Oct 6, 2024
1 parent 700365d commit d854e41
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,
"rust-analyzer.imports.granularity.group": "module"
"rust-analyzer.imports.granularity.group": "module",
"rust-analyzer.imports.group.enable": false
}
2 changes: 2 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ edition = "2021"
proc-macro = true

[dependencies]
memmap2 = "0.9.5"
object = "0.36.5"
proc-macro2 = "1.0.86"
quote = "1.0.37"
syn = "2.0.79"
Expand Down
10 changes: 9 additions & 1 deletion macros/src/cpp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use self::class::Class;
use crate::META;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::parse::{Parse, ParseStream};
use syn::Error;

mod class;
mod func;
Expand All @@ -20,8 +22,14 @@ pub fn render(items: Declarations) -> syn::Result<TokenStream> {
}

fn render_class(item: Class) -> syn::Result<TokenStream> {
// Render constructors.
// Get metadata.
let class = item.name;
let meta = match META.get_type("_ZN7cppbind9type_infoI6class1E4sizeE") {
Some(v) => v,
None => return Err(Error::new_spanned(class, "type_info not found")),
};

// Render constructors.
let mut impls = TokenStream::new();

for (i, ctor) in item.ctors.into_iter().enumerate() {
Expand Down
1 change: 1 addition & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use syn::{parse_macro_input, Error};

mod cpp;
mod meta;
mod symbol;

/// Generate binding to C++ functions and methods.
#[proc_macro]
Expand Down
59 changes: 56 additions & 3 deletions macros/src/meta.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
pub use self::ty::*;

use memmap2::Mmap;
use object::read::archive::ArchiveFile;
use std::collections::HashMap;
use std::fs::File;
use std::path::Path;
use thiserror::Error;

mod ty;

/// Contains C++ metadata loaded from a static library.
pub struct Metadata {}
pub struct Metadata {
types: HashMap<String, TypeInfo>,
}

impl Metadata {
pub fn from_static_lib(path: impl AsRef<Path>) -> Result<Self, MetadataError> {
Ok(Self {})
// Open file.
let file = File::open(path).map_err(MetadataError::OpenFileFailed)?;
let file = unsafe { Mmap::map(&file).map_err(MetadataError::MapFileFailed) }?;
let file = ArchiveFile::parse(file.as_ref()).map_err(MetadataError::ParseFileFailed)?;

// Get symbols.
let symbols = file.symbols().map_err(MetadataError::GetSymbolsFailed)?;
let mut types = HashMap::new();
let symbols = match symbols {
Some(v) => v,
None => return Ok(Self { types }),
};

// Parse symbols.
for sym in symbols {
let sym = sym.map_err(MetadataError::ReadSymbolFailed)?;
let sym = match std::str::from_utf8(sym.name()) {
Ok(v) => v,
Err(_) => continue, // Ignore unknown symbol.
};

types.insert(sym.to_owned(), TypeInfo::new());
}

Ok(Self { types })
}

pub fn get_type(&self, name: impl AsRef<str>) -> Option<&TypeInfo> {
self.types.get(name.as_ref())
}
}

/// Represents an error when [`Metadata`] fails to load.
#[derive(Debug, Error)]
pub enum MetadataError {}
pub enum MetadataError {
#[error("couldn't open the specified file")]
OpenFileFailed(#[source] std::io::Error),

#[error("couldn't map the specified file")]
MapFileFailed(#[source] std::io::Error),

#[error("couldn't parse the specified file")]
ParseFileFailed(#[source] object::read::Error),

#[error("couldn't get available symbols")]
GetSymbolsFailed(#[source] object::read::Error),

#[error("couldn't read a symbol")]
ReadSymbolFailed(#[source] object::read::Error),
}
8 changes: 8 additions & 0 deletions macros/src/meta/ty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Contains information for a C++ class.
pub struct TypeInfo {}

impl TypeInfo {
pub(super) fn new() -> Self {
Self {}
}
}
14 changes: 14 additions & 0 deletions macros/src/symbol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use thiserror::Error;

/// C++ symbol.
pub struct Symbol {}

impl Symbol {
pub fn parse(mangled: impl AsRef<[u8]>) -> Result<Self, SymbolError> {
Ok(Self {})
}
}

/// Represents an error when [`Symbol`] fails to parse from a mangled name.
#[derive(Debug, Error)]
pub enum SymbolError {}

0 comments on commit d854e41

Please sign in to comment.