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
17 changes: 5 additions & 12 deletions tasks/ast_tools/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,18 @@ pub trait Derive: Runner {

let mut crate_contents = FxHashMap::<&str, CrateContent>::default();
for type_def in &schema.types {
let derived = match type_def {
let (derived, file_id) = match type_def {
TypeDef::Struct(struct_def) if struct_def.generates_derive(derive_id) => {
self.derive(StructOrEnum::Struct(struct_def), schema)
let derived = self.derive(StructOrEnum::Struct(struct_def), schema);
(derived, struct_def.file_id)
}
TypeDef::Enum(enum_def) if enum_def.generates_derive(derive_id) => {
self.derive(StructOrEnum::Enum(enum_def), schema)
let derived = self.derive(StructOrEnum::Enum(enum_def), schema);
(derived, enum_def.file_id)
}
_ => continue,
};

let file_id = type_def.file_id().unwrap();
let content = crate_contents.entry(schema.files[file_id].krate()).or_default();
content.import_file_ids.insert(file_id);

Expand Down Expand Up @@ -278,14 +279,6 @@ impl Def for StructOrEnum<'_> {
}
}

/// Get [`FileId`] of file containing definition of this type.
fn file_id(&self) -> Option<FileId> {
match self {
Self::Struct(struct_def) => struct_def.file_id(),
Self::Enum(enum_def) => enum_def.file_id(),
}
}

/// Get all traits which have derives generated for this type.
fn generated_derives(&self) -> Derives {
match self {
Expand Down
9 changes: 1 addition & 8 deletions tasks/ast_tools/src/schema/defs/box.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::TokenStream;
use quote::quote;

use super::{extensions::layout::Layout, Def, Derives, FileId, Schema, TypeDef, TypeId};
use super::{extensions::layout::Layout, Def, Derives, Schema, TypeDef, TypeId};

/// Type definition for a `Box`.
#[derive(Debug)]
Expand Down Expand Up @@ -40,13 +40,6 @@ impl Def for BoxDef {
&self.name
}

/// Get [`FileId`] of file containing definition of this type.
///
/// `Box`es are not defined in a file, so returns `None`.
fn file_id(&self) -> Option<FileId> {
None
}

/// Get all traits which have derives generated for this type.
///
/// `Box`es never have any generated derives.
Expand Down
9 changes: 1 addition & 8 deletions tasks/ast_tools/src/schema/defs/cell.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::TokenStream;
use quote::quote;

use super::{extensions::layout::Layout, Def, Derives, FileId, Schema, TypeDef, TypeId};
use super::{extensions::layout::Layout, Def, Derives, Schema, TypeDef, TypeId};

/// Type definition for a `Cell`.
#[derive(Debug)]
Expand Down Expand Up @@ -40,13 +40,6 @@ impl Def for CellDef {
&self.name
}

/// Get [`FileId`] of file containing definition of this type.
///
/// `Cell`s are not defined in a file, so returns `None`.
fn file_id(&self) -> Option<FileId> {
None
}

/// Get all traits which have derives generated for this type.
///
/// `Cell`s never have any generated derives.
Expand Down
13 changes: 7 additions & 6 deletions tasks/ast_tools/src/schema/defs/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
layout::Layout,
visit::{VisitEnum, VisitFieldOrVariant},
},
Def, Derives, FileId, Schema, TypeDef, TypeId,
Def, Derives, File, FileId, Schema, TypeDef, TypeId,
};

pub type Discriminant = u8;
Expand Down Expand Up @@ -100,6 +100,12 @@ impl EnumDef {
self.layout.layout_64.size == 1
}

/// Get the [`File`] which this struct is defined in.
#[expect(dead_code)]
pub fn file<'s>(&self, schema: &'s Schema) -> &'s File {
&schema.files[self.file_id]
}

/// Get iterator over variant indexes.
///
/// Only includes own variant, not inherited.
Expand All @@ -124,11 +130,6 @@ impl Def for EnumDef {
&self.name
}

/// Get [`FileId`] of file containing definition of this type.
fn file_id(&self) -> Option<FileId> {
Some(self.file_id)
}

/// Get all traits which have derives generated for this type.
fn generated_derives(&self) -> Derives {
self.generated_derives
Expand Down
9 changes: 5 additions & 4 deletions tasks/ast_tools/src/schema/defs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::Ident;

use crate::{codegen::DeriveId, utils::create_ident, Schema};
use crate::{
codegen::DeriveId,
schema::{File, Schema},
utils::create_ident,
};

use super::{extensions, Derives, FileId, TypeId};

Expand Down Expand Up @@ -32,9 +36,6 @@ pub trait Def {
/// Get type name.
fn name(&self) -> &str;

/// Get [`FileId`] of file containing definition of this type.
fn file_id(&self) -> Option<FileId>;

/// Get all traits which have derives generated for this type.
fn generated_derives(&self) -> Derives;

Expand Down
9 changes: 1 addition & 8 deletions tasks/ast_tools/src/schema/defs/option.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::TokenStream;
use quote::quote;

use super::{extensions::layout::Layout, Def, Derives, FileId, Schema, TypeDef, TypeId};
use super::{extensions::layout::Layout, Def, Derives, Schema, TypeDef, TypeId};

/// Type definition for an `Option`.
#[derive(Debug)]
Expand Down Expand Up @@ -40,13 +40,6 @@ impl Def for OptionDef {
&self.name
}

/// Get [`FileId`] of file containing definition of this type.
///
/// `Options`s are not defined in a file, so returns `None`.
fn file_id(&self) -> Option<FileId> {
None
}

/// Get all traits which have derives generated for this type.
///
/// `Option`s never have any generated derives.
Expand Down
9 changes: 1 addition & 8 deletions tasks/ast_tools/src/schema/defs/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::TokenStream;
use quote::quote;

use super::{extensions::layout::Layout, Def, Derives, FileId, Schema, TypeDef, TypeId};
use super::{extensions::layout::Layout, Def, Derives, Schema, TypeDef, TypeId};

/// Type definition for a primitive type.
///
Expand Down Expand Up @@ -33,13 +33,6 @@ impl Def for PrimitiveDef {
self.name
}

/// Get [`FileId`] of file containing definition of this type.
///
/// Primitives are not defined in a file, so returns `None`.
fn file_id(&self) -> Option<FileId> {
None
}

/// Get all traits which have derives generated for this type.
///
/// Primitives never have any generated derives.
Expand Down
13 changes: 7 additions & 6 deletions tasks/ast_tools/src/schema/defs/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
span::SpanStruct,
visit::{VisitFieldOrVariant, VisitStruct},
},
Def, Derives, FileId, Schema, TypeDef, TypeId,
Def, Derives, File, FileId, Schema, TypeDef, TypeId,
};

/// Type definition for a struct.
Expand Down Expand Up @@ -77,6 +77,12 @@ impl StructDef {
self.plural_name().to_case(Case::Snake)
}

/// Get the [`File`] which this struct is defined in.
#[expect(dead_code)]
pub fn file<'s>(&self, schema: &'s Schema) -> &'s File {
&schema.files[self.file_id]
}

/// Get iterator over field indexes.
pub fn field_indices(&self) -> Range<usize> {
0..self.fields.len()
Expand All @@ -94,11 +100,6 @@ impl Def for StructDef {
&self.name
}

/// Get [`FileId`] of file containing definition of this type.
fn file_id(&self) -> Option<FileId> {
Some(self.file_id)
}

/// Get all traits which have derives generated for this type.
fn generated_derives(&self) -> Derives {
self.generated_derives
Expand Down
19 changes: 2 additions & 17 deletions tasks/ast_tools/src/schema/defs/type.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use proc_macro2::TokenStream;

use super::{
BoxDef, CellDef, Def, Derives, EnumDef, FileId, OptionDef, PrimitiveDef, Schema, StructDef,
TypeId, VecDef,
BoxDef, CellDef, Def, Derives, EnumDef, OptionDef, PrimitiveDef, Schema, StructDef, TypeId,
VecDef,
};

/// Type definition for a type.
Expand Down Expand Up @@ -44,21 +44,6 @@ impl Def for TypeDef {
}
}

/// Get [`FileId`] of file containing definition of this type.
///
/// Returns `None` if type is not defined in a file (e.g. primitives).
fn file_id(&self) -> Option<FileId> {
match self {
TypeDef::Struct(def) => def.file_id(),
TypeDef::Enum(def) => def.file_id(),
TypeDef::Primitive(def) => def.file_id(),
TypeDef::Option(def) => def.file_id(),
TypeDef::Box(def) => def.file_id(),
TypeDef::Vec(def) => def.file_id(),
TypeDef::Cell(def) => def.file_id(),
}
}

/// Get all traits which have derives generated for this type.
fn generated_derives(&self) -> Derives {
match self {
Expand Down
9 changes: 1 addition & 8 deletions tasks/ast_tools/src/schema/defs/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use quote::quote;

use super::{
extensions::{layout::Layout, visit::VisitVec},
Def, Derives, FileId, Schema, TypeDef, TypeId,
Def, Derives, Schema, TypeDef, TypeId,
};

/// Type definition for a `Vec`.
Expand Down Expand Up @@ -50,13 +50,6 @@ impl Def for VecDef {
&self.name
}

/// Get [`FileId`] of file containing definition of this type.
///
/// `Vec`s are not defined in a file, so returns `None`.
fn file_id(&self) -> Option<FileId> {
None
}

/// Get all traits which have derives generated for this type.
///
/// `Vec`s never have any generated derives.
Expand Down
Loading