Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion xdr_codegen/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub struct XdrEnum {
pub struct XdrStruct {
// TODO: store snake_case -> CameCase transformed name...
pub name: String,
pub members: Vec<Declaration>,
pub members: Vec<NamedDeclaration>,

/// Structs that have an optional "pointer" to themselves at the end need special handling
/// during codegen. This field is filled in during Schema::validate().
Expand Down
4 changes: 0 additions & 4 deletions xdr_codegen/src/codegen/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ impl XdrStruct {
buf.code_block("pub fn serialize_alloc(&self) -> Vec<u8>", |buf| {
buf.add_line("let mut buf = Vec::new();");
for decl in self.members.iter() {
let Declaration::Named(decl) = decl else {
buf.add_line("// void");
continue;
};
buf.add_line(&format!("// {}:", decl.name));
decl.serialize_inline(None, Context::NotInUnion, buf, tab);
}
Expand Down
4 changes: 0 additions & 4 deletions xdr_codegen/src/codegen/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ impl XdrStruct {
"pub fn deserialize(&mut self, input: &mut &[u8]) -> Result<(), helpers::DeserializeError>",
|buf| {
for decl in self.members.iter() {
let Declaration::Named(decl) = decl else {
buf.add_line("// void");
continue;
};
buf.add_line(&format!("// {}:", decl.name));
decl.deserialize_inline(None, buf, tab);
}
Expand Down
6 changes: 0 additions & 6 deletions xdr_codegen/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,6 @@ impl XdrStruct {
buf.type_header();
buf.code_block(&format!("pub struct {}", self.name), |buf| {
for decl in self.members.iter() {
let Declaration::Named(decl) = decl else {
unimplemented!("'void' is not supported as a struct member");
};
self.member_declaration(decl, buf, tab);
}
});
Expand All @@ -660,9 +657,6 @@ impl XdrStruct {
buf.code_block("fn default() -> Self", |buf| {
buf.code_block(&self.name, |buf| {
for decl in self.members.iter() {
let Declaration::Named(decl) = decl else {
unimplemented!("'void' is not supported as a struct member");
};
buf.add_line(&format!("{}: {},", decl.name, decl.default_value(tab)));
}
});
Expand Down
4 changes: 0 additions & 4 deletions xdr_codegen/src/codegen/no_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ impl XdrStruct {
buf.code_block("pub fn serialize(&self, buf: &mut [u8]) -> usize", |buf| {
buf.add_line("let mut offset = 0;");
for decl in &self.members {
let Declaration::Named(decl) = decl else {
buf.add_line("// void");
continue;
};
buf.add_line(&format!("// {}:", decl.name));
decl.serialize_no_alloc_inline(None, buf, tab);
}
Expand Down
8 changes: 6 additions & 2 deletions xdr_codegen/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,19 @@ impl<'src> Parser<'src> {
variants
}

fn xdr_struct_body(&mut self) -> Vec<Declaration> {
fn xdr_struct_body(&mut self) -> Vec<NamedDeclaration> {
self.expect(TokenKind::LeftBrace, "struct body must start with '{'");
let mut members = Vec::new();
loop {
if self.peek().kind == TokenKind::RightBrace {
self.next();
break;
}
members.push(self.declaration());
let decl = self.declaration();
let Declaration::Named(n) = decl else {
panic!("Invalid to have void declaration as a struct member.");
};
members.push(n);
self.expect(TokenKind::Semicolon, "Expected ';' following declaration");
}

Expand Down
52 changes: 28 additions & 24 deletions xdr_codegen/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,35 @@ impl XdrStruct {
///
/// This is recursive because a declaration might refer to a typedef, which might in turn refer to
/// an optional `outer_name`.
fn is_declaration_option_of_name(outer_name: &str, decl: &Declaration, tab: &SymbolTable) -> bool {
match decl {
Declaration::Named(n) => match &n.kind {
DeclarationKind::Optional(ty) => {
let XdrType::Name(member_type_name) = ty else {
return false;
};
if *member_type_name != outer_name {
return false;
}
true
fn is_declaration_option_of_name(
outer_name: &str,
n: &NamedDeclaration,
tab: &SymbolTable,
) -> bool {
match &n.kind {
DeclarationKind::Optional(ty) => {
let XdrType::Name(member_type_name) = ty else {
return false;
};
if *member_type_name != outer_name {
return false;
}
DeclarationKind::Scalar(ty) => {
let XdrType::Name(name) = ty else {
return false;
};
let def = tab.lookup_definition(name).expect("Undefined name");
let Definition::TypeDef(ref typedef) = *def else {
return false;
};
is_declaration_option_of_name(outer_name, &typedef.decl, tab)
}
DeclarationKind::Array(_) => false,
},
Declaration::Void => false,
true
}
DeclarationKind::Scalar(ty) => {
let XdrType::Name(name) = ty else {
return false;
};
let def = tab.lookup_definition(name).expect("Undefined name");
let Definition::TypeDef(ref typedef) = *def else {
return false;
};
let Declaration::Named(ref n) = typedef.decl else {
return false;
};
is_declaration_option_of_name(outer_name, n, tab)
}
DeclarationKind::Array(_) => false,
}
}

Expand Down
Loading