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

feat(tasks/ast_codegen): prototype for codegen AST related code #3815

Merged
merged 1 commit into from
Jun 25, 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
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ oxc_macros = { path = "crates/oxc_macros" }
oxc_linter = { path = "crates/oxc_linter" }
oxc_prettier = { path = "crates/oxc_prettier" }
oxc_tasks_common = { path = "tasks/common" }
oxc_ast_codegen = { path = "tasks/oxc_ast_codegen" }

napi = "2.16.6"
napi-derive = "2.16.5"
Expand Down Expand Up @@ -179,11 +180,12 @@ base64-simd = "0.8"
cfg-if = "1.0.0"
schemars = "0.8.21"
oxc-browserslist = "1.0.1"
prettyplease = "0.2.20"
criterion2 = { version = "0.11.0", default-features = false }
daachorse = { version = "1.0.0" }

[workspace.metadata.cargo-shear]
ignored = ["napi", "oxc_traverse"]
ignored = ["napi", "oxc_traverse", "oxc_ast_codegen", "prettyplease"]
overlookmotel marked this conversation as resolved.
Show resolved Hide resolved

[profile.dev]
# Disabling debug info speeds up local and CI builds,
Expand Down
26 changes: 26 additions & 0 deletions tasks/ast_codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "oxc_ast_codegen"
version = "0.0.0"
publish = false
edition.workspace = true
license.workspace = true

[lints]
workspace = true


[[bin]]
name = "oxc_ast_codegen"
test = false

[dependencies]
syn = { workspace = true, features = ["full", "extra-traits", "clone-impls", "derive", "parsing", "printing", "proc-macro"] }
quote = { workspace = true }
proc-macro2 = { workspace = true }
itertools = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
prettyplease = { workspace = true }

[package.metadata.cargo-shear]
ignored = ["prettyplease"]
115 changes: 115 additions & 0 deletions tasks/ast_codegen/src/defs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use super::{REnum, RStruct, RType};
use crate::{schema::Inherit, TypeName};
use quote::ToTokens;
use serde::Serialize;

#[derive(Debug, Serialize)]
pub enum TypeDef {
Struct(StructDef),
Enum(EnumDef),
}

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

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

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

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

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

impl From<&RType> for Option<TypeDef> {
fn from(rtype: &RType) -> Self {
match rtype {
RType::Enum(it) => Some(TypeDef::Enum(it.into())),
RType::Struct(it) => Some(TypeDef::Struct(it.into())),
_ => None,
}
}
}

impl From<&REnum> for EnumDef {
fn from(it @ REnum { item, meta }: &REnum) -> Self {
Self {
name: it.ident().to_string(),
variants: item.variants.iter().map(Into::into).collect(),
has_lifetime: item.generics.lifetimes().count() > 0,
inherits: meta.inherits.iter().map(Into::into).collect(),
}
}
}

impl From<&RStruct> for StructDef {
fn from(it @ RStruct { item, .. }: &RStruct) -> Self {
Self {
name: it.ident().to_string(),
fields: item.fields.iter().map(Into::into).collect(),
has_lifetime: item.generics.lifetimes().count() > 0,
}
}
}

impl From<&syn::Variant> for EnumVariantDef {
fn from(variant: &syn::Variant) -> Self {
Self {
name: variant.ident.to_string(),
discriminant: variant.discriminant.as_ref().map(|(_, disc)| match disc {
syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }) => {
lit.base10_parse().expect("invalid base10 enum discriminant")
}
_ => panic!("invalid enum discriminant"),
}),
fields: variant.fields.iter().map(Into::into).collect(),
}
}
}

impl From<&Inherit> for EnumInheritDef {
fn from(inherit: &Inherit) -> Self {
match inherit {
Inherit::Linked { super_, variants } => Self {
super_name: super_.into(),
variants: variants.iter().map(Into::into).collect(),
},
Inherit::Unlinked(_) => {
panic!("`Unlinked` inherits can't be converted to a valid `EnumInheritDef`!")
}
}
}
}

impl From<&syn::Field> for FieldDef {
fn from(field: &syn::Field) -> Self {
Self {
name: field.ident.as_ref().map(ToString::to_string),
r#type: field.ty.to_token_stream().to_string().replace(' ', ""),
}
}
}
17 changes: 17 additions & 0 deletions tasks/ast_codegen/src/generators/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use quote::ToTokens;

use crate::{CodegenCtx, Generator, GeneratorOutput};

pub struct AstGenerator;

impl Generator for AstGenerator {
fn name(&self) -> &'static str {
"AstGenerator"
}

fn generate(&mut self, ctx: &CodegenCtx) -> GeneratorOutput {
let output =
ctx.modules.iter().map(|it| (it.module.clone(), it.to_token_stream())).collect();
GeneratorOutput::Many(output)
}
}
33 changes: 33 additions & 0 deletions tasks/ast_codegen/src/generators/ast_kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use itertools::Itertools;
use syn::{parse_quote, Variant};

use crate::{schema::RType, CodegenCtx, Generator, GeneratorOutput};

pub struct AstKindGenerator;

impl Generator for AstKindGenerator {
fn name(&self) -> &'static str {
"AstKindGenerator"
}

fn generate(&mut self, ctx: &CodegenCtx) -> GeneratorOutput {
let kinds: Vec<Variant> = ctx
.ty_table
.iter()
.filter_map(|maybe_kind| match &*maybe_kind.borrow() {
kind @ (RType::Enum(_) | RType::Struct(_)) => {
let ident = kind.ident();
let typ = kind.as_type();
Some(parse_quote!(#ident(#typ)))
}
_ => None,
})
.collect_vec();

GeneratorOutput::One(parse_quote! {
pub enum AstKind<'a> {
#(#kinds),*
}
})
}
}
5 changes: 5 additions & 0 deletions tasks/ast_codegen/src/generators/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod ast;
mod ast_kind;

pub use ast::AstGenerator;
pub use ast_kind::AstKindGenerator;
67 changes: 67 additions & 0 deletions tasks/ast_codegen/src/linker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::collections::VecDeque;

use super::{CodegenCtx, Cow, Inherit, Itertools, RType, Result};

pub trait Linker<'a> {
fn link(&'a self, linker: impl FnMut(&mut RType, &'a Self) -> Result<bool>) -> Result<&'a ()>;
}

impl<'a> Linker<'a> for CodegenCtx {
fn link(
&'a self,
mut linker: impl FnMut(&mut RType, &'a Self) -> Result<bool>,
) -> Result<&'a ()> {
let mut unresolved = self.ident_table.keys().collect::<VecDeque<_>>();
while let Some(next) = unresolved.pop_back() {
let next_id = *self.type_id(next).unwrap();

let val = &mut self.ty_table[next_id].borrow_mut();

if !linker(val, self)? {
// for now we don't have entangled dependencies so we just add unresolved item back
// to the list so we revisit it again at the end.
unresolved.push_front(next);
}
}
Ok(&())
}
}

/// Returns false if can't resolve
/// TODO: right now we don't resolve nested inherits, return is always true for now.
/// # Panics
/// On invalid inheritance.
#[allow(clippy::unnecessary_wraps)]
pub fn linker(ty: &mut RType, ctx: &CodegenCtx) -> Result<bool> {
// Exit early if it isn't an enum, We only link to resolve enum inheritance!
let RType::Enum(ty) = ty else {
return Ok(true);
};

// Exit early if there is this enum doesn't use enum inheritance
if ty.meta.inherits.is_empty() {
return Ok(true);
}

ty.meta.inherits = ty
.meta
.inherits
.drain(..)
.map(|it| match it {
Inherit::Unlinked(it) => {
let linkee = ctx.find(&Cow::Owned(it.to_string())).unwrap();
let variants = match &*linkee.borrow() {
RType::Enum(enum_) => enum_.item.variants.clone(),
_ => {
panic!("invalid inheritance, you can only inherit from enums and in enums.")
}
};
ty.item.variants.extend(variants.clone());
Inherit::Linked { super_: it.clone(), variants }
}
Inherit::Linked { .. } => it,
})
.collect_vec();

Ok(true)
}
Loading
Loading