-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initializes parser for Itanium symbol
- Loading branch information
1 parent
d854e41
commit 329ab4f
Showing
4 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
use std::borrow::Cow; | ||
use thiserror::Error; | ||
|
||
mod itanium; | ||
|
||
/// C++ symbol. | ||
pub struct Symbol {} | ||
pub struct Symbol { | ||
name: Vec<Segment<'static>>, | ||
} | ||
|
||
impl Symbol { | ||
pub fn parse(mangled: impl AsRef<[u8]>) -> Result<Self, SymbolError> { | ||
Ok(Self {}) | ||
let mangled = mangled.as_ref(); | ||
|
||
if mangled.starts_with(b"_Z") { | ||
self::itanium::parse(&mangled[2..]) | ||
} else { | ||
todo!() | ||
} | ||
} | ||
|
||
pub fn name(&self) -> &[Segment] { | ||
&self.name | ||
} | ||
} | ||
|
||
/// Segment of a C++ name. | ||
#[derive(PartialEq, Eq)] | ||
pub enum Segment<'a> { | ||
Ident(Cow<'a, str>), | ||
TemplateArg(TemplateArg<'a>), | ||
} | ||
|
||
/// Argument of a template instantiation. | ||
#[derive(PartialEq, Eq)] | ||
pub enum TemplateArg<'a> { | ||
Ident(Cow<'a, str>), | ||
} | ||
|
||
/// Represents an error when [`Symbol`] fails to parse from a mangled name. | ||
#[derive(Debug, Error)] | ||
pub enum SymbolError {} | ||
pub enum SymbolError { | ||
#[error("unknown symbol")] | ||
UnknownSymbol, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use super::{Segment, Symbol, SymbolError, TemplateArg}; | ||
use std::cmp::min; | ||
use std::iter::Peekable; | ||
use std::slice::Iter; | ||
|
||
pub fn parse(mangled: &[u8]) -> Result<Symbol, SymbolError> { | ||
let mut name = Vec::new(); | ||
let mut iter = mangled.iter().peekable(); | ||
|
||
match *iter.next().ok_or(SymbolError::UnknownSymbol)? { | ||
b'N' => parse_nested_name(&mut name, &mut iter)?, | ||
_ => return Err(SymbolError::UnknownSymbol), | ||
} | ||
|
||
Ok(Symbol { name }) | ||
} | ||
|
||
fn parse_nested_name( | ||
segments: &mut Vec<Segment>, | ||
iter: &mut Peekable<Iter<u8>>, | ||
) -> Result<(), SymbolError> { | ||
loop { | ||
let b = *iter.next().ok_or(SymbolError::UnknownSymbol)?; | ||
|
||
match b { | ||
b'0' => return Err(SymbolError::UnknownSymbol), // Identifier with zero length? | ||
b'1'..=b'9' => segments.push(Segment::Ident(parse_source_name(iter, b)?.into())), | ||
b'I' => parse_template_args(segments, iter)?, | ||
b'E' => break, | ||
_ => return Err(SymbolError::UnknownSymbol), | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn parse_source_name(iter: &mut Peekable<Iter<u8>>, first: u8) -> Result<String, SymbolError> { | ||
// Get length. | ||
let mut len = Into::<usize>::into(first - b'0'); | ||
|
||
while let Some(&b) = iter.next_if(|b| b.is_ascii_digit()) { | ||
len = len | ||
.checked_mul(10) | ||
.and_then(move |v| v.checked_add((b - b'0').into())) | ||
.ok_or(SymbolError::UnknownSymbol)?; | ||
} | ||
|
||
// This ABI does not yet specify a mangling for identifiers containing characters outside of | ||
// _A-Za-z0-9. | ||
let mut name = String::with_capacity(min(len, 128)); | ||
|
||
for _ in 0..len { | ||
// We don't need to handle unicode here due to the above rule. | ||
let b = *iter.next().ok_or(SymbolError::UnknownSymbol)?; | ||
|
||
name.push(b.into()); | ||
} | ||
|
||
Ok(name) | ||
} | ||
|
||
fn parse_template_args( | ||
segments: &mut Vec<Segment>, | ||
iter: &mut Peekable<Iter<u8>>, | ||
) -> Result<(), SymbolError> { | ||
loop { | ||
let b = *iter.next().ok_or(SymbolError::UnknownSymbol)?; | ||
let a = match b { | ||
b'E' => break, | ||
b'0' => return Err(SymbolError::UnknownSymbol), // Identifier with zero length? | ||
b'1'..=b'9' => TemplateArg::Ident(parse_source_name(iter, b)?.into()), | ||
b'X' => todo!("expression"), | ||
b'L' => todo!("simple expressions"), | ||
b'J' => todo!("argument pack"), | ||
_ => todo!(), | ||
}; | ||
|
||
segments.push(Segment::TemplateArg(a)); | ||
} | ||
|
||
Ok(()) | ||
} |