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
36 changes: 8 additions & 28 deletions tasks/ast_tools/src/generators/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use std::{borrow::Cow, stringify};

use convert_case::{Case, Casing};
use itertools::Itertools;
use lazy_static::lazy_static;
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use rustc_hash::FxHashMap;
use syn::{parse_quote, Ident, Type};

use crate::{
Expand Down Expand Up @@ -231,25 +229,13 @@ fn generate_enum_from_variant_builder_fn(
}

fn default_init_field(field: &FieldDef) -> bool {
macro_rules! field {
($ident:ident: $ty:ty) => {
(stringify!($ident), stringify!($ty))
};
}
lazy_static! {
static ref DEFAULT_FIELDS: FxHashMap<&'static str, &'static str> = FxHashMap::from_iter([
field!(scope_id: Cell<Option<ScopeId>>),
field!(symbol_id: Cell<Option<SymbolId>>),
field!(reference_id: Cell<Option<ReferenceId>>),
]);
}

let ident = field.ident().expect("expected named field");
if let Some(&default_type) = DEFAULT_FIELDS.get(ident.to_string().as_str()) {
default_type == field.typ.raw()
} else {
false
}
matches!(
(ident.to_string().as_str(), field.typ.raw()),
("scope_id", "Cell<Option<ScopeId>>")
| ("symbol_id", "Cell<Option<SymbolId>>")
| ("reference_id", "Cell<Option<ReferenceId>>")
)
}

/// Generate builder function for struct.
Expand Down Expand Up @@ -543,25 +529,19 @@ fn article_for<S: AsRef<str>>(word: S) -> &'static str {

impl ToTokens for DocComment<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
macro_rules! newline {
() => {
tokens.extend(quote!( #[doc = ""]));
};
}

let summary = &self.summary;
tokens.extend(quote!( #[doc = #summary]));

// print description
for line in &self.description {
// extra newline needed to create a new paragraph
newline!();
tokens.extend(quote!( #[doc = ""]));
tokens.extend(quote!( #[doc = #line]));
}

// print docs for function parameters
if !self.params.is_empty() {
newline!();
tokens.extend(quote!( #[doc = ""]));
tokens.extend(quote!( #[doc = " ## Parameters"]));
for param in self.params {
let docs = param.docs.first();
Expand Down
38 changes: 19 additions & 19 deletions tasks/ast_tools/src/rust_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,17 @@ impl AstType {
}

pub fn set_visitable(&mut self, value: bool) -> Result<()> {
macro_rules! assign {
($it:ident) => {{
debug_assert!($it.meta.ast, "only ast types can be visitable!");
$it.meta.visitable = value;
}};
}
match self {
AstType::Enum(it) => assign!(it),
AstType::Struct(it) => assign!(it),
AstType::Macro(it) => return Err(unexpanded_macro_err(&it.item)),
}
AstType::Enum(enum_) => {
debug_assert!(enum_.meta.ast, "only AST types can be visitable!");
enum_.meta.visitable = value;
}
AstType::Struct(struct_) => {
debug_assert!(struct_.meta.ast, "only AST types can be visitable!");
struct_.meta.visitable = value;
}
AstType::Macro(macro_) => return Err(unexpanded_macro_err(&macro_.item)),
};
Ok(())
}

Expand Down Expand Up @@ -245,16 +245,16 @@ impl AstType {
}

pub fn set_layout(&mut self, layout_64: Layout, layout_32: Layout) -> Result<()> {
macro_rules! assign {
($it:ident) => {{
$it.meta.layout_32 = layout_32;
$it.meta.layout_64 = layout_64;
}};
}
match self {
AstType::Enum(it) => assign!(it),
AstType::Struct(it) => assign!(it),
AstType::Macro(it) => return Err(unexpanded_macro_err(&it.item)),
AstType::Enum(enum_) => {
enum_.meta.layout_32 = layout_32;
enum_.meta.layout_64 = layout_64;
}
AstType::Struct(struct_) => {
struct_.meta.layout_32 = layout_32;
struct_.meta.layout_64 = layout_64;
}
AstType::Macro(macro_) => return Err(unexpanded_macro_err(&macro_.item)),
}
Ok(())
}
Expand Down
27 changes: 21 additions & 6 deletions tasks/ast_tools/src/schema/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
TypeId,
};

use super::{with_either, TypeName};
use super::TypeName;

#[derive(Debug, Serialize)]
#[serde(untagged)]
Expand All @@ -21,19 +21,31 @@ pub enum TypeDef {

impl TypeDef {
pub fn id(&self) -> TypeId {
with_either!(self, it => it.id)
match self {
TypeDef::Struct(def) => def.id,
TypeDef::Enum(def) => def.id,
}
}

pub fn name(&self) -> &str {
with_either!(self, it => &it.name)
match self {
TypeDef::Struct(def) => &def.name,
TypeDef::Enum(def) => &def.name,
}
}

pub fn visitable(&self) -> bool {
with_either!(self, it => it.visitable)
match self {
TypeDef::Struct(def) => def.visitable,
TypeDef::Enum(def) => def.visitable,
}
}

pub fn generated_derives(&self) -> &Vec<String> {
with_either!(self, it => &it.generated_derives)
match self {
TypeDef::Struct(def) => &def.generated_derives,
TypeDef::Enum(def) => &def.generated_derives,
}
}

pub fn generates_derive(&self, derive: &str) -> bool {
Expand All @@ -42,7 +54,10 @@ impl TypeDef {
}

pub fn module_path(&self) -> &str {
with_either!(self, it => &it.module_path)
match self {
TypeDef::Struct(def) => &def.module_path,
TypeDef::Enum(def) => &def.module_path,
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions tasks/ast_tools/src/schema/get_generics.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use syn::{parse_quote, Generics};

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

pub trait GetGenerics {
fn has_lifetime(&self) -> bool {
Expand All @@ -21,7 +18,10 @@ pub trait GetGenerics {

impl GetGenerics for TypeDef {
fn has_lifetime(&self) -> bool {
with_either!(self, it => it.has_lifetime())
match self {
TypeDef::Struct(def) => def.has_lifetime(),
TypeDef::Enum(def) => def.has_lifetime(),
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions tasks/ast_tools/src/schema/get_ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use syn::Ident;

use crate::util::ToIdent;

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

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

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

Expand Down
11 changes: 0 additions & 11 deletions tasks/ast_tools/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,3 @@ fn parse_generate_derive(attrs: &[Attribute]) -> Vec<String> {
}
Vec::from_iter(derives)
}

macro_rules! with_either {
($def:expr, $it:ident => $body:expr) => {
match $def {
TypeDef::Struct($it) => $body,
TypeDef::Enum($it) => $body,
}
};
}

use with_either;
14 changes: 6 additions & 8 deletions tasks/ast_tools/src/schema/to_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ impl ToType for TypeRef {
}
}

auto_impl_to_type! {
TypeDef,
EnumDef,
StructDef,
}

macro_rules! auto_impl_to_type {
macro_rules! impl_to_type {
($($ty:ty,)+) => (
$(
impl ToType for $ty {
Expand All @@ -55,4 +49,8 @@ macro_rules! auto_impl_to_type {
)
}

use auto_impl_to_type;
impl_to_type! {
TypeDef,
EnumDef,
StructDef,
}