Skip to content

ctest: Add extraction of relevant types. #4485

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

Merged
merged 1 commit into from
Jun 16, 2025
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
2 changes: 1 addition & 1 deletion ctest-next/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ publish = false

[dependencies]
cc = "1.2.25"
syn = { version = "2.0.101", features = ["full", "visit", "visit-mut", "fold"] }
syn = { version = "2.0.101", features = ["full", "visit", "extra-traits"] }
20 changes: 20 additions & 0 deletions ctest-next/src/ast/constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::BoxStr;

/// Represents a constant variable defined in Rust.
#[derive(Debug, Clone)]
pub struct Const {
#[expect(unused)]
pub(crate) public: bool,
pub(crate) ident: BoxStr,
#[expect(unused)]
pub(crate) ty: syn::Type,
#[expect(unused)]
pub(crate) expr: syn::Expr,
}

impl Const {
/// Return the identifier of the constant as a string.
pub fn ident(&self) -> &str {
&self.ident
}
}
18 changes: 18 additions & 0 deletions ctest-next/src/ast/field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::BoxStr;

/// Represents a field in a struct or union defined in Rust.
#[derive(Debug, Clone)]
pub struct Field {
#[expect(unused)]
pub(crate) public: bool,
pub(crate) ident: BoxStr,
#[expect(unused)]
pub(crate) ty: syn::Type,
}

impl Field {
/// Return the identifier of the field as a string if it exists.
pub fn ident(&self) -> &str {
&self.ident
}
}
24 changes: 24 additions & 0 deletions ctest-next/src/ast/function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::{Abi, BoxStr, Parameter};

/// Represents a function signature defined in Rust.
///
/// This structure is only used for parsing functions in extern blocks.
#[derive(Debug, Clone)]
pub struct Fn {
#[expect(unused)]
pub(crate) public: bool,
#[expect(unused)]
pub(crate) abi: Abi,
pub(crate) ident: BoxStr,
#[expect(unused)]
pub(crate) parameters: Vec<Parameter>,
#[expect(unused)]
pub(crate) return_type: Option<syn::Type>,
}

impl Fn {
/// Return the identifier of the function as a string.
pub fn ident(&self) -> &str {
&self.ident
}
}
59 changes: 59 additions & 0 deletions ctest-next/src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
mod constant;
mod field;
mod function;
mod parameter;
mod static_variable;
mod structure;
mod type_alias;
mod union;

pub use constant::Const;
pub use field::Field;
pub use function::Fn;
pub use parameter::Parameter;
pub use static_variable::Static;
pub use structure::Struct;
pub use type_alias::Type;
pub use union::Union;

/// The ABI as defined by the extern block.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Abi {
/// The C ABI.
C,
/// The Rust ABI.
Rust,
/// Any other ABI.
Other(String),
}

impl From<&str> for Abi {
fn from(value: &str) -> Self {
match value.to_lowercase().as_str() {
"c" => Abi::C,
"rust" => Abi::Rust,
s => Abi::Other(s.to_string()),
}
}
}

/// Things that can appear directly inside of a module or scope.
///
/// This is not an exhaustive list and only contains variants directly useful
/// for our purposes.
#[derive(Debug, Clone)]
#[expect(unused)]
pub(crate) enum Item {
/// Represents a constant defined in Rust.
Const(Const),
/// Represents a function defined in Rust.
Fn(Fn),
/// Represents a static variable defined in Rust.
Static(Static),
/// Represents a type alias defined in Rust.
Type(Type),
/// Represents a struct defined in Rust.
Struct(Struct),
/// Represents a union defined in Rust.
Union(Union),
}
8 changes: 8 additions & 0 deletions ctest-next/src/ast/parameter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Represents a parameter in a function signature defined in Rust.
#[derive(Debug, Clone)]
pub struct Parameter {
#[expect(unused)]
pub(crate) pattern: syn::Pat,
#[expect(unused)]
pub(crate) ty: syn::Type,
}
23 changes: 23 additions & 0 deletions ctest-next/src/ast/static_variable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::{Abi, BoxStr};

/// Represents a static variable in Rust.
///
/// This structure is only used for parsing statics in extern blocks,
/// as a result it does not have a field for storing the expression.
#[derive(Debug, Clone)]
pub struct Static {
#[expect(unused)]
pub(crate) public: bool,
#[expect(unused)]
pub(crate) abi: Abi,
pub(crate) ident: BoxStr,
#[expect(unused)]
pub(crate) ty: syn::Type,
}

impl Static {
/// Return the identifier of the static variable as a string.
pub fn ident(&self) -> &str {
&self.ident
}
}
18 changes: 18 additions & 0 deletions ctest-next/src/ast/structure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::{BoxStr, Field};

/// Represents a struct defined in Rust.
#[derive(Debug, Clone)]
pub struct Struct {
#[expect(unused)]
pub(crate) public: bool,
pub(crate) ident: BoxStr,
#[expect(unused)]
pub(crate) fields: Vec<Field>,
}

impl Struct {
/// Return the identifier of the struct as a string.
pub fn ident(&self) -> &str {
&self.ident
}
}
18 changes: 18 additions & 0 deletions ctest-next/src/ast/type_alias.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::BoxStr;

/// Represents a type alias defined in Rust.
#[derive(Debug, Clone)]
pub struct Type {
#[expect(unused)]
pub(crate) public: bool,
pub(crate) ident: BoxStr,
#[expect(unused)]
pub(crate) ty: syn::Type,
}

impl Type {
/// Return the identifier of the type alias as a string.
pub fn ident(&self) -> &str {
&self.ident
}
}
18 changes: 18 additions & 0 deletions ctest-next/src/ast/union.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::{BoxStr, Field};

/// Represents a union defined in Rust.
#[derive(Debug, Clone)]
pub struct Union {
#[expect(unused)]
pub(crate) public: bool,
pub(crate) ident: BoxStr,
#[expect(unused)]
pub(crate) fields: Vec<Field>,
}

impl Union {
/// Return the identifier of the union as a string.
pub fn ident(&self) -> &str {
&self.ident
}
}
Loading