Skip to content

Commit

Permalink
refactor(ast_tools): shorten code by hoisting imports (#6903)
Browse files Browse the repository at this point in the history
Pure refactor.
  • Loading branch information
overlookmotel committed Oct 25, 2024
1 parent 07dcc0c commit 45333bc
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 54 deletions.
11 changes: 6 additions & 5 deletions tasks/ast_tools/src/derives/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use convert_case::{Case, Casing};
use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::quote;
use rustc_hash::{FxHashMap, FxHashSet};
use syn::{parse_str, ItemUse};

use crate::{
codegen::LateCtx,
Expand Down Expand Up @@ -49,15 +51,14 @@ pub trait Derive {
.chain(it.strip_suffix("::mod").unwrap_or(it).split("::").skip(1))
.chain(["*"])
.join("::");
let use_module: syn::ItemUse =
syn::parse_str(format!("use {local_path};").as_str()).unwrap();
quote::quote! {
let use_module: ItemUse = parse_str(format!("use {local_path};").as_str()).unwrap();
quote! {
///@@line_break
#use_module
}
});

quote::quote! {
quote! {
#prelude

#(#use_modules)*
Expand Down Expand Up @@ -103,7 +104,7 @@ pub trait Derive {
tokens: Self::template(
modules,
streams.into_iter().fold(TokenStream::new(), |mut acc, it| {
acc.extend(quote::quote! {
acc.extend(quote! {
///@@line_break
});
acc.extend(it);
Expand Down
4 changes: 2 additions & 2 deletions tasks/ast_tools/src/markers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use syn::{
parenthesized,
parse::{Parse, ParseStream},
parse2,
punctuated::Punctuated,
punctuated::{self, Punctuated},
spanned::Spanned,
token, Attribute, Expr, Ident, LitStr, Meta, MetaNameValue, Token,
};
Expand Down Expand Up @@ -38,7 +38,7 @@ impl Parse for VisitArg {
pub struct VisitArgs(Punctuated<VisitArg, Token![,]>);

impl IntoIterator for VisitArgs {
type IntoIter = syn::punctuated::IntoIter<Self::Item>;
type IntoIter = punctuated::IntoIter<Self::Item>;
type Item = VisitArg;

fn into_iter(self) -> Self::IntoIter {
Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_tools/src/output/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn generate_header(generator_path: &str) -> TokenStream {

// TODO: Add generation date, AST source hash, etc here.
let edit_comment = format!("@ To edit this generated file you have to edit `{generator_path}`");
quote::quote! {
quote! {
//!@ Auto-generated code, DO NOT EDIT DIRECTLY!
#![doc = #edit_comment]
//!@@line_break
Expand Down
5 changes: 3 additions & 2 deletions tasks/ast_tools/src/schema/defs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::Serialize;
use syn::Ident;

use super::{with_either, TypeName};
use crate::{
Expand Down Expand Up @@ -120,7 +121,7 @@ pub struct VariantDef {
}

impl VariantDef {
pub fn ident(&self) -> syn::Ident {
pub fn ident(&self) -> Ident {
self.name.to_ident()
}

Expand Down Expand Up @@ -176,7 +177,7 @@ impl From<&syn::Visibility> for Visibility {
}

impl FieldDef {
pub fn ident(&self) -> Option<syn::Ident> {
pub fn ident(&self) -> Option<Ident> {
self.name.as_ref().map(ToIdent::to_ident)
}
}
Expand Down
4 changes: 2 additions & 2 deletions tasks/ast_tools/src/schema/get_generics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use syn::parse_quote;
use syn::{parse_quote, Generics};

use super::{
defs::{EnumDef, StructDef, TypeDef},
Expand All @@ -10,7 +10,7 @@ pub trait GetGenerics {
false
}

fn generics(&self) -> Option<syn::Generics> {
fn generics(&self) -> Option<Generics> {
if self.has_lifetime() {
Some(parse_quote!(<'a>))
} else {
Expand Down
10 changes: 6 additions & 4 deletions tasks/ast_tools/src/schema/get_ident.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
use syn::Ident;

use super::{
defs::{EnumDef, StructDef, TypeDef},
with_either,
};
use crate::util::ToIdent;

pub trait GetIdent {
fn ident(&self) -> syn::Ident;
fn ident(&self) -> Ident;
}

impl GetIdent for TypeDef {
fn ident(&self) -> syn::Ident {
fn ident(&self) -> Ident {
with_either!(self, it => it.ident())
}
}

impl GetIdent for StructDef {
fn ident(&self) -> syn::Ident {
fn ident(&self) -> Ident {
self.name.to_ident()
}
}

impl GetIdent for EnumDef {
fn ident(&self) -> syn::Ident {
fn ident(&self) -> Ident {
self.name.to_ident()
}
}
50 changes: 23 additions & 27 deletions tasks/ast_tools/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ use std::fmt;
use quote::ToTokens;
use rustc_hash::FxHashSet;
use serde::Serialize;
use syn::{
punctuated::Punctuated, Attribute, Expr, ExprLit, Field, Ident, Lit, Meta, MetaNameValue,
Token, Type, Variant,
};

use crate::{
codegen,
codegen::EarlyCtx,
layout::KnownLayout,
markers::{
get_derive_attributes, get_estree_attribute, get_scope_attribute, get_scope_markers,
Expand Down Expand Up @@ -121,18 +125,18 @@ impl<'a> IntoIterator for &'a Schema {
}
}

fn parse_struct_outer_markers(attrs: &Vec<syn::Attribute>) -> Result<StructOuterMarkers> {
fn parse_struct_outer_markers(attrs: &Vec<Attribute>) -> Result<StructOuterMarkers> {
Ok(StructOuterMarkers {
scope: get_scope_attribute(attrs).transpose()?,
estree: get_estree_attribute(attrs).transpose()?,
})
}

fn parse_enum_outer_markers(attrs: &Vec<syn::Attribute>) -> Result<EnumOuterMarkers> {
fn parse_enum_outer_markers(attrs: &Vec<Attribute>) -> Result<EnumOuterMarkers> {
Ok(EnumOuterMarkers { estree: get_estree_attribute(attrs).transpose()?.unwrap_or_default() })
}

fn parse_inner_markers(attrs: &Vec<syn::Attribute>) -> Result<InnerMarkers> {
fn parse_inner_markers(attrs: &Vec<Attribute>) -> Result<InnerMarkers> {
Ok(InnerMarkers {
span: attrs.iter().any(|a| a.path().is_ident("span")),
visit: get_visit_markers(attrs)?,
Expand All @@ -142,7 +146,7 @@ fn parse_inner_markers(attrs: &Vec<syn::Attribute>) -> Result<InnerMarkers> {
}

// lower `AstType` to `TypeDef`.
pub fn lower_ast_types(ctx: &codegen::EarlyCtx) -> Schema {
pub fn lower_ast_types(ctx: &EarlyCtx) -> Schema {
let defs = ctx
.mods()
.borrow()
Expand All @@ -153,15 +157,15 @@ pub fn lower_ast_types(ctx: &codegen::EarlyCtx) -> Schema {
Schema { defs }
}

fn lower_ast_type(ty: &rust::AstType, ctx: &codegen::EarlyCtx) -> TypeDef {
fn lower_ast_type(ty: &rust::AstType, ctx: &EarlyCtx) -> TypeDef {
match ty {
rust::AstType::Enum(it) => TypeDef::Enum(lower_ast_enum(it, ctx)),
rust::AstType::Struct(it) => TypeDef::Struct(lower_ast_struct(it, ctx)),
rust::AstType::Macro(it) => panic!("{}", unexpanded_macro_err(&it.item)),
}
}

fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &codegen::EarlyCtx) -> EnumDef {
fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &EarlyCtx) -> EnumDef {
let (size_64, align_64, offsets_64) = meta
.layout_64
.clone()
Expand Down Expand Up @@ -199,10 +203,7 @@ fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &codegen::Ea
}
}

fn lower_ast_struct(
it @ rust::Struct { item, meta }: &rust::Struct,
ctx: &codegen::EarlyCtx,
) -> StructDef {
fn lower_ast_struct(it @ rust::Struct { item, meta }: &rust::Struct, ctx: &EarlyCtx) -> StructDef {
let (size_64, align_64, offsets_64) = meta
.layout_64
.clone()
Expand Down Expand Up @@ -234,7 +235,7 @@ fn lower_ast_struct(
}
}

fn lower_variant<F>(variant: &syn::Variant, enum_dbg_name: F, ctx: &codegen::EarlyCtx) -> VariantDef
fn lower_variant<F>(variant: &Variant, enum_dbg_name: F, ctx: &EarlyCtx) -> VariantDef
where
F: Fn() -> String,
{
Expand All @@ -243,7 +244,7 @@ where
discriminant: variant.discriminant.as_ref().map_or_else(
|| panic!("expected explicit enum discriminants on {}", enum_dbg_name()),
|(_, disc)| match disc {
syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }) => {
Expr::Lit(ExprLit { lit: Lit::Int(lit), .. }) => {
lit.base10_parse().expect("invalid base10 enum discriminant")
}
_ => panic!("invalid enum discriminant {:?} on {}", disc, enum_dbg_name()),
Expand All @@ -254,7 +255,7 @@ where
}
}

fn lower_inherit(inherit: &rust::Inherit, ctx: &codegen::EarlyCtx) -> InheritDef {
fn lower_inherit(inherit: &rust::Inherit, ctx: &EarlyCtx) -> InheritDef {
match inherit {
rust::Inherit::Linked { super_, variants } => InheritDef {
super_: create_type_ref(super_, ctx),
Expand All @@ -269,7 +270,7 @@ fn lower_inherit(inherit: &rust::Inherit, ctx: &codegen::EarlyCtx) -> InheritDef
}
}

fn lower_field(field: &syn::Field, ctx: &codegen::EarlyCtx) -> FieldDef {
fn lower_field(field: &Field, ctx: &EarlyCtx) -> FieldDef {
FieldDef {
name: field
.ident
Expand All @@ -282,7 +283,7 @@ fn lower_field(field: &syn::Field, ctx: &codegen::EarlyCtx) -> FieldDef {
}
}

fn create_type_ref(ty: &syn::Type, ctx: &codegen::EarlyCtx) -> TypeRef {
fn create_type_ref(ty: &Type, ctx: &EarlyCtx) -> TypeRef {
let ident = ty.get_ident();
let id = ident.as_ident().and_then(|id| ctx.type_id(&id.to_string()));
let transparent_id = ctx.type_id(&ident.inner_ident().to_string());
Expand All @@ -296,21 +297,16 @@ fn create_type_ref(ty: &syn::Type, ctx: &codegen::EarlyCtx) -> TypeRef {
}
}

fn get_docs(attrs: &[syn::Attribute]) -> Vec<String> {
fn get_docs(attrs: &[Attribute]) -> Vec<String> {
attrs
.iter()
.filter_map(|attr| {
if let syn::Meta::NameValue(syn::MetaNameValue {
path,
value: syn::Expr::Lit(lit),
..
}) = &attr.meta
{
if let Meta::NameValue(MetaNameValue { path, value: Expr::Lit(lit), .. }) = &attr.meta {
if !path.is_ident("doc") {
return None;
}
match &lit.lit {
syn::Lit::Str(lit) => Some(lit.value().trim().to_string()),
Lit::Str(lit) => Some(lit.value().trim().to_string()),
_ => None,
}
} else {
Expand All @@ -320,15 +316,15 @@ fn get_docs(attrs: &[syn::Attribute]) -> Vec<String> {
.collect()
}

fn parse_generate_derive(attrs: &[syn::Attribute]) -> Vec<String> {
fn parse_generate_derive(attrs: &[Attribute]) -> Vec<String> {
let mut derives = FxHashSet::default();
for attr in attrs {
if !attr.path().is_ident("generate_derive") {
continue;
}

let args: syn::punctuated::Punctuated<syn::Ident, syn::Token![,]> =
attr.parse_args_with(syn::punctuated::Punctuated::parse_terminated).unwrap();
let args: Punctuated<Ident, Token![,]> =
attr.parse_args_with(Punctuated::parse_terminated).unwrap();

for arg in args {
derives.insert(arg.to_string());
Expand Down
22 changes: 11 additions & 11 deletions tasks/ast_tools/src/schema/to_type.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::parse_quote;
use syn::{parse_quote, parse_str, Type};

use super::{
defs::{EnumDef, StructDef, TypeDef, TypeRef},
GetGenerics, GetIdent,
};

pub trait ToType {
fn to_type(&self) -> syn::Type;
fn to_type_elide(&self) -> syn::Type;
fn to_type_with_explicit_generics(&self, generics: TokenStream) -> syn::Type;
fn to_type(&self) -> Type;
fn to_type_elide(&self) -> Type;
fn to_type_with_explicit_generics(&self, generics: TokenStream) -> Type;
}

impl ToType for TypeRef {
fn to_type(&self) -> syn::Type {
syn::parse_str(self.raw()).unwrap()
fn to_type(&self) -> Type {
parse_str(self.raw()).unwrap()
}

fn to_type_elide(&self) -> syn::Type {
fn to_type_elide(&self) -> Type {
self.to_type_with_explicit_generics(proc_macro2::TokenStream::default())
}

fn to_type_with_explicit_generics(&self, generics: proc_macro2::TokenStream) -> syn::Type {
fn to_type_with_explicit_generics(&self, generics: proc_macro2::TokenStream) -> Type {
let ident = self.name().first_ident();
parse_quote!(#ident #generics)
}
Expand All @@ -38,15 +38,15 @@ macro_rules! auto_impl_to_type {
($($ty:ty,)+) => (
$(
impl ToType for $ty {
fn to_type(&self) -> syn::Type {
fn to_type(&self) -> Type {
self.to_type_with_explicit_generics(self.generics().to_token_stream())
}

fn to_type_elide(&self) -> syn::Type {
fn to_type_elide(&self) -> Type {
self.to_type_with_explicit_generics(TokenStream::default())
}

fn to_type_with_explicit_generics(&self, generics: TokenStream) -> syn::Type {
fn to_type_with_explicit_generics(&self, generics: TokenStream) -> Type {
let name = self.ident();
parse_quote!(#name #generics)
}
Expand Down

0 comments on commit 45333bc

Please sign in to comment.