Skip to content

Commit c66b9eb

Browse files
committed
section-mangler: Add prefix constant, derive Debug and PartialEq
1 parent 5ffee18 commit c66b9eb

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

compiler/section_mangler/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,20 @@ mod parser;
4949
/// The main builder type of this crate. Use it to create mangling contexts, in
5050
/// order to encode and decode binary type information.
5151
// TODO: Add example code for using this builder
52+
#[derive(Debug, PartialEq)]
5253
pub enum SectionMangler {
5354
Function(FunctionMangler),
5455
Variable(VariableMangler),
5556
}
5657

58+
#[derive(Debug, PartialEq)]
5759
pub struct FunctionMangler {
5860
name: String,
5961
parameters: Vec<FunctionArgument>,
6062
return_type: Option<Type>,
6163
}
6264

65+
#[derive(Debug, PartialEq)]
6366
pub struct VariableMangler {
6467
name: String,
6568
ty: Type,
@@ -113,6 +116,7 @@ impl SectionMangler {
113116
// NOTE: This is called `variable_linkage` in the `MemberInfo` struct.
114117

115118
/// We have to encode this because if it changes, the function needs to be reloaded - this is an ABI breakage
119+
#[derive(Debug, PartialEq)]
116120
pub enum FunctionArgument {
117121
ByValue(Type),
118122
ByRef(Type),
@@ -129,6 +133,7 @@ impl fmt::Display for FunctionArgument {
129133
}
130134

131135
// TODO: Do we have to encode this? Does that affect ABI? Probably
136+
#[derive(Debug, PartialEq)]
132137
pub enum StringEncoding {
133138
// TODO: Should we encode this differently? this could cause problems compared to encoding unsigned types
134139
/// Encoded as `8u`
@@ -148,6 +153,7 @@ impl fmt::Display for StringEncoding {
148153

149154
// This maps directly to the [`DataTypeInformation`] enum in RuSTy - we simply remove some fields and add the ability to encode/decode serialize/deserialize
150155
// TODO: Do we have to handle Generic?
156+
#[derive(Debug, PartialEq)]
151157
pub enum Type {
152158
/// Encoded as `v`
153159
Void,
@@ -237,16 +243,18 @@ impl fmt::Display for Type {
237243
}
238244
}
239245

246+
pub const PREFIX: &str = "$RUSTY$";
247+
240248
// TODO: How to encode variadics?
241249
fn mangle_function(FunctionMangler { name, parameters, return_type }: FunctionMangler) -> String {
242250
let mangled = parameters
243251
.into_iter()
244252
/* FIXME: Is that correct? */
245253
.fold(return_type.unwrap_or(Type::Void).to_string(), |mangled, arg| format!("{mangled}[{arg}]"));
246254

247-
format!("{name}:{mangled}")
255+
format!("{PREFIX}{name}:{mangled}")
248256
}
249257

250258
fn mangle_variable(VariableMangler { name, ty }: VariableMangler) -> String {
251-
format!("{name}:{ty}")
259+
format!("{PREFIX}{name}:{ty}")
252260
}

compiler/section_mangler/src/parser.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fn parse_prefix(input: &str) -> ParseResult<Prefix> {
1818
let fn_prefix = tag("fn").map(|_| Prefix::Fn);
1919
let var_prefix = tag("var").map(|_| Prefix::Var);
2020

21+
let (input, _) = tag(crate::PREFIX)(input)?;
2122
let (input, prefix) = alt((fn_prefix, var_prefix))(input)?;
2223

2324
Ok((input, prefix))
@@ -87,8 +88,12 @@ mod tests {
8788

8889
#[test]
8990
fn parse_prefix_valid() {
90-
assert!(parse_prefix("fn").is_ok());
91-
assert!(parse_prefix("var").is_ok());
91+
assert!(parse_prefix("$RUSTY$fn").is_ok());
92+
assert!(parse_prefix("$RUSTY$var").is_ok());
93+
94+
assert!(parse_prefix("$RUSTY$random").is_err());
95+
assert!(parse_prefix("fn").is_err());
96+
assert!(parse_prefix("var").is_err());
9297

9398
assert!(parse_prefix("").is_err());
9499
assert!(parse_prefix("a random prefix").is_err());
@@ -138,4 +143,9 @@ mod tests {
138143
assert!(type_pointer("p").is_err());
139144
assert!(type_pointer("i0").is_err());
140145
}
146+
147+
#[test]
148+
fn parse_variable() {
149+
SectionMangler::from("$RUSTY$var-name:u8");
150+
}
141151
}

0 commit comments

Comments
 (0)