Skip to content

Commit

Permalink
refactor(ast_codegen): move away from RType in generators. (#4682)
Browse files Browse the repository at this point in the history
This PR introduces `EarlyCtx` and `LateCtx` in place of the old `CodegenCtx`, Early passes operate at the AST level while generators and other late passes operate on the schema.

It will also replace the confusing `RType` name with something more idiomatic ~~(open for suggestions, I haven't found a good name yet)~~ I've named it `AstType` and dropped the `R` prefix for `REnum` and `RStruct`.

There are some qualities of life improvements too, Things like `to_type_elide` can be used to simplify the code.

Related to #4442 (and can potentially mark it "close as fixed").
  • Loading branch information
rzvxa committed Aug 6, 2024
1 parent e24fb5b commit 1690a57
Show file tree
Hide file tree
Showing 20 changed files with 1,034 additions and 737 deletions.
170 changes: 0 additions & 170 deletions tasks/ast_codegen/src/defs.rs

This file was deleted.

21 changes: 6 additions & 15 deletions tasks/ast_codegen/src/generators/assert_layouts.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{PathArguments, Type};
use syn::Type;

use crate::{
defs::{FieldDef, TypeDef},
output, CodegenCtx, Generator, GeneratorOutput,
output,
schema::{FieldDef, ToType, TypeDef},
Generator, GeneratorOutput, LateCtx,
};

use super::{define_generator, generated_header};
Expand All @@ -18,23 +19,13 @@ impl Generator for AssertLayouts {
stringify!(AssertLayouts)
}

fn generate(&mut self, ctx: &CodegenCtx) -> GeneratorOutput {
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
let (assertions_64, assertions_32) = ctx
.schema
.borrow()
.definitions
.iter()
.map(|def| {
let typ =
ctx.find(def.name()).and_then(|ty| ty.borrow().as_type()).map(|mut ty| {
if let Type::Path(ty) = &mut ty {
if let Some(seg) = ty.path.segments.first_mut() {
seg.arguments = PathArguments::None;
}
}
ty
});
let typ = typ.unwrap();
let typ = def.to_type_elide();
assert_type(&typ, def)
})
.collect::<(Vec<TokenStream>, Vec<TokenStream>)>();
Expand Down
Loading

0 comments on commit 1690a57

Please sign in to comment.