Skip to content

Commit f5621c6

Browse files
committed
ctest: Add extraction of relevant types.
1 parent 9c8cd59 commit f5621c6

File tree

15 files changed

+542
-15
lines changed

15 files changed

+542
-15
lines changed

ctest-next/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ publish = false
99

1010
[dependencies]
1111
cc = "1.2.25"
12-
syn = { version = "2.0.101", features = ["full", "visit", "visit-mut", "fold"] }
12+
syn = { version = "2.0.101", features = ["full", "visit", "extra-traits"] }

ctest-next/src/ast/constant.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::BoxStr;
2+
3+
/// Represents a constant variable defined in Rust.
4+
#[derive(Debug, Clone)]
5+
pub struct Const {
6+
#[expect(unused)]
7+
pub(crate) public: bool,
8+
pub(crate) ident: BoxStr,
9+
#[expect(unused)]
10+
pub(crate) ty: syn::Type,
11+
#[expect(unused)]
12+
pub(crate) expr: syn::Expr,
13+
}
14+
15+
impl Const {
16+
/// Return the identifier of the constant as a string.
17+
pub fn ident(&self) -> &str {
18+
&self.ident
19+
}
20+
}

ctest-next/src/ast/field.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::BoxStr;
2+
3+
/// Represents a field in a struct or union defined in Rust.
4+
#[derive(Debug, Clone)]
5+
pub struct Field {
6+
#[expect(unused)]
7+
pub(crate) public: bool,
8+
pub(crate) ident: BoxStr,
9+
#[expect(unused)]
10+
pub(crate) ty: syn::Type,
11+
}
12+
13+
impl Field {
14+
/// Return the identifier of the field as a string if it exists.
15+
pub fn ident(&self) -> &str {
16+
&self.ident
17+
}
18+
}

ctest-next/src/ast/function.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::{Abi, BoxStr, Parameter};
2+
3+
/// Represents a function signature defined in Rust.
4+
///
5+
/// This structure is only used for parsing functions in extern blocks.
6+
#[derive(Debug, Clone)]
7+
pub struct Fn {
8+
#[expect(unused)]
9+
pub(crate) public: bool,
10+
#[expect(unused)]
11+
pub(crate) abi: Abi,
12+
pub(crate) ident: BoxStr,
13+
#[expect(unused)]
14+
pub(crate) parameters: Vec<Parameter>,
15+
#[expect(unused)]
16+
pub(crate) return_type: Option<syn::Type>,
17+
}
18+
19+
impl Fn {
20+
/// Return the identifier of the function as a string.
21+
pub fn ident(&self) -> &str {
22+
&self.ident
23+
}
24+
}

ctest-next/src/ast/mod.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
mod constant;
2+
mod field;
3+
mod function;
4+
mod parameter;
5+
mod static_variable;
6+
mod structure;
7+
mod type_alias;
8+
mod union;
9+
10+
pub use constant::Const;
11+
pub use field::Field;
12+
pub use function::Fn;
13+
pub use parameter::Parameter;
14+
pub use static_variable::Static;
15+
pub use structure::Struct;
16+
pub use type_alias::Type;
17+
pub use union::Union;
18+
19+
/// The ABI as defined by the extern block.
20+
#[derive(Debug, Clone, PartialEq, Eq)]
21+
pub enum Abi {
22+
/// The C ABI.
23+
C,
24+
/// The Rust ABI.
25+
Rust,
26+
/// Any other ABI.
27+
Other(String),
28+
}
29+
30+
impl From<&str> for Abi {
31+
fn from(value: &str) -> Self {
32+
match value.to_lowercase().as_str() {
33+
"c" => Abi::C,
34+
"rust" => Abi::Rust,
35+
s => Abi::Other(s.to_string()),
36+
}
37+
}
38+
}
39+
40+
/// Things that can appear directly inside of a module or scope.
41+
///
42+
/// This is not an exhaustive list and only contains variants directly useful
43+
/// for our purposes.
44+
#[derive(Debug, Clone)]
45+
#[expect(unused)]
46+
pub(crate) enum Item {
47+
/// Represents a constant defined in Rust.
48+
Const(Const),
49+
/// Represents a function defined in Rust.
50+
Fn(Fn),
51+
/// Represents a static variable defined in Rust.
52+
Static(Static),
53+
/// Represents a type alias defined in Rust.
54+
Type(Type),
55+
/// Represents a struct defined in Rust.
56+
Struct(Struct),
57+
/// Represents a union defined in Rust.
58+
Union(Union),
59+
}

ctest-next/src/ast/parameter.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// Represents a parameter in a function signature defined in Rust.
2+
#[derive(Debug, Clone)]
3+
pub struct Parameter {
4+
#[expect(unused)]
5+
pub(crate) pattern: syn::Pat,
6+
#[expect(unused)]
7+
pub(crate) ty: syn::Type,
8+
}

ctest-next/src/ast/static_variable.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::{Abi, BoxStr};
2+
3+
/// Represents a static variable in Rust.
4+
///
5+
/// This structure is only used for parsing statics in extern blocks,
6+
/// as a result it does not have a field for storing the expression.
7+
#[derive(Debug, Clone)]
8+
pub struct Static {
9+
#[expect(unused)]
10+
pub(crate) public: bool,
11+
#[expect(unused)]
12+
pub(crate) abi: Abi,
13+
pub(crate) ident: BoxStr,
14+
#[expect(unused)]
15+
pub(crate) ty: syn::Type,
16+
}
17+
18+
impl Static {
19+
/// Return the identifier of the static variable as a string.
20+
pub fn ident(&self) -> &str {
21+
&self.ident
22+
}
23+
}

ctest-next/src/ast/structure.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::{BoxStr, Field};
2+
3+
/// Represents a struct defined in Rust.
4+
#[derive(Debug, Clone)]
5+
pub struct Struct {
6+
#[expect(unused)]
7+
pub(crate) public: bool,
8+
pub(crate) ident: BoxStr,
9+
#[expect(unused)]
10+
pub(crate) fields: Vec<Field>,
11+
}
12+
13+
impl Struct {
14+
/// Return the identifier of the struct as a string.
15+
pub fn ident(&self) -> &str {
16+
&self.ident
17+
}
18+
}

ctest-next/src/ast/type_alias.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::BoxStr;
2+
3+
/// Represents a type alias defined in Rust.
4+
#[derive(Debug, Clone)]
5+
pub struct Type {
6+
#[expect(unused)]
7+
pub(crate) public: bool,
8+
pub(crate) ident: BoxStr,
9+
#[expect(unused)]
10+
pub(crate) ty: syn::Type,
11+
}
12+
13+
impl Type {
14+
/// Return the identifier of the type alias as a string.
15+
pub fn ident(&self) -> &str {
16+
&self.ident
17+
}
18+
}

ctest-next/src/ast/union.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::{BoxStr, Field};
2+
3+
/// Represents a union defined in Rust.
4+
#[derive(Debug, Clone)]
5+
pub struct Union {
6+
#[expect(unused)]
7+
pub(crate) public: bool,
8+
pub(crate) ident: BoxStr,
9+
#[expect(unused)]
10+
pub(crate) fields: Vec<Field>,
11+
}
12+
13+
impl Union {
14+
/// Return the identifier of the union as a string.
15+
pub fn ident(&self) -> &str {
16+
&self.ident
17+
}
18+
}

0 commit comments

Comments
 (0)