Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ast_codegen): cleanup schema generated by ast_codegen. #4403

Merged
merged 1 commit into from
Aug 2, 2024
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
28 changes: 14 additions & 14 deletions tasks/ast_codegen/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@ pub enum TypeDef {

#[derive(Debug, Serialize)]
pub struct StructDef {
name: TypeName,
fields: Vec<FieldDef>,
has_lifetime: bool,
pub name: TypeName,
pub fields: Vec<FieldDef>,
pub has_lifetime: bool,
}

#[derive(Debug, Serialize)]
pub struct EnumDef {
name: TypeName,
variants: Vec<EnumVariantDef>,
pub name: TypeName,
pub variants: Vec<EnumVariantDef>,
/// For `@inherits` inherited enum variants
inherits: Vec<EnumInheritDef>,
has_lifetime: bool,
pub inherits: Vec<EnumInheritDef>,
pub has_lifetime: bool,
}

#[derive(Debug, Serialize)]
pub struct EnumVariantDef {
name: TypeName,
fields: Vec<FieldDef>,
discriminant: Option<u8>,
pub name: TypeName,
pub fields: Vec<FieldDef>,
pub discriminant: Option<u8>,
}

#[derive(Debug, Serialize)]
pub struct EnumInheritDef {
super_name: String,
variants: Vec<EnumVariantDef>,
pub super_name: String,
pub variants: Vec<EnumVariantDef>,
}

#[derive(Debug, Serialize)]
pub struct FieldDef {
/// `None` if unnamed
name: Option<String>,
r#type: TypeName,
pub name: Option<String>,
pub r#type: TypeName,
}

impl From<&RType> for Option<TypeDef> {
Expand Down
12 changes: 6 additions & 6 deletions tasks/ast_codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ impl GeneratorOutput {
struct CodegenCtx {
ty_table: TypeTable,
ident_table: IdentTable,
schema: Vec<Schema>,
schema: Schema,
}

struct CodegenResult {
/// One schema per definition file
schema: Vec<Schema>,
schema: Schema,
outputs: Vec<(/* generator name */ &'static str, /* output */ GeneratorOutput)>,
}

Expand All @@ -126,9 +125,10 @@ impl CodegenCtx {
}
}

let mut me = Self { ty_table, ident_table, schema: Vec::default() }.link(linker)?;
let schema = mods.into_iter().map(Module::build).collect::<Result<Vec<_>>>()?;
_ = std::mem::replace(&mut me.schema, schema);
let mut me = Self { ty_table, ident_table, schema: Schema::default() }.link(linker)?;
for m in mods {
m.build_in(&mut me.schema)?;
}
Ok(me)
}

Expand Down
19 changes: 5 additions & 14 deletions tasks/ast_codegen/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ use crate::{util::NormalizeError, TypeName};

use super::{parse_file, Itertools, PathBuf, Rc, Read, RefCell, Result, TypeDef, TypeRef};

#[derive(Debug, serde::Serialize)]
#[derive(Debug, Default, serde::Serialize)]
pub struct Schema {
source: PathBuf,
definitions: Definitions,
}

#[derive(Debug, serde::Serialize)]
pub struct Definitions {
types: Vec<TypeDef>,
definitions: Vec<TypeDef>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -265,16 +259,13 @@ impl Module {
Ok(self)
}

pub fn build(self) -> Result<Schema> {
pub fn build_in(self, schema: &mut Schema) -> Result<()> {
if !self.loaded {
return Err(String::from(LOAD_ERROR));
}

let definitions = Definitions {
// We filter map to get rid of stuff we don't need in our schema.
types: self.items.into_iter().filter_map(|it| (&*it.borrow()).into()).collect(),
};
Ok(Schema { source: self.path, definitions })
schema.definitions.extend(self.items.into_iter().filter_map(|it| (&*it.borrow()).into()));
Ok(())
}
}

Expand Down
Loading