Skip to content

Macro syn rewrite #1073

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

Draft
wants to merge 27 commits into
base: rust-next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6b2262f
kbuild: rust: apply `CONFIG_WERROR` to hostprogs as well
ojeda Apr 7, 2024
a5f77c5
kbuild: rust: use shared host Rust flags for `macros`
ojeda Apr 7, 2024
e4527ff
kbuild: rust-analyzer: support key-value `cfg`s
ojeda Apr 10, 2024
c3b22c6
rust: proc-macro2: import crate
ojeda Oct 9, 2022
617c120
rust: proc-macro2: add SPDX License Identifiers
ojeda Oct 9, 2022
24954e2
rust: proc-macro2: remove `unicode_ident` dependency
ojeda Oct 9, 2022
925b30e
rust: quote: import crate
ojeda Oct 9, 2022
cdad907
rust: quote: add SPDX License Identifiers
ojeda Oct 9, 2022
328f151
rust: syn: import crate
ojeda Oct 9, 2022
cef2d41
rust: syn: add SPDX License Identifiers
ojeda Oct 9, 2022
82e9a4f
rust: syn: remove `unicode-ident` dependency
ojeda Oct 9, 2022
a59391f
rust: Kbuild: enable `proc-macro2`, `quote` and `syn`
ojeda Oct 9, 2022
1b729e1
rust: macros: fix soundness issue in `module!` macro
y86-dev Apr 1, 2024
ed6e1a8
rust: macros: replace `quote!` with `quote::quote` and use `proc-macro2`
y86-dev Apr 6, 2024
8938c4f
rust: macros: rewrite `#[vtable]` using `syn`
y86-dev Apr 6, 2024
638dc79
rust: macros: rewrite `module!` using `syn`
y86-dev Apr 6, 2024
bdb4cff
rust: macros: rewrite `Zeroable` derive macro using `syn`
y86-dev Apr 6, 2024
2a88e8a
rust: macros: rewrite `#[pin_data]` using `syn`
y86-dev Apr 6, 2024
b8459ad
rust: macros: rewrite `#[pinned_drop]` using `syn`
y86-dev Apr 5, 2024
5d4eb2d
rust: macros: rewrite `__internal_init!` using `syn`
y86-dev Apr 6, 2024
a8dae43
rust: macros: remove helpers
y86-dev Apr 6, 2024
45d057b
rust: init: remove macros.rs
y86-dev Apr 8, 2024
c7790b6
fixup! rust: macros: rewrite `#[pin_data]` using `syn`
y86-dev Apr 16, 2024
66b9b59
fixup! rust: macros: rewrite `#[pinned_drop]` using `syn`
y86-dev Apr 16, 2024
8d21a65
fixup! rust: macros: rewrite `__internal_init!` using `syn`
y86-dev Apr 16, 2024
edd1ce0
fixup! rust: macros: rewrite `Zeroable` derive macro using `syn`
y86-dev Apr 16, 2024
6ad858d
fixup! rust: macros: rewrite `#[vtable]` using `syn`
y86-dev Apr 16, 2024
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
Prev Previous commit
Next Next commit
rust: macros: replace quote! with quote::quote and use proc-macro2
We now have access to the `quote` crate providing the `quote!` macro
that does the same (and more) as our own `quote!` macro. Thus replace
our own version with the one from the crate.
Since the `quote` crate uses the `proc-macro2` library, we also use that
in our macros instead of `proc-macro`.

Signed-off-by: Benno Lossin <benno.lossin@proton.me>
  • Loading branch information
y86-dev committed Apr 10, 2024
commit ed6e1a89e96d4857cf5a98d180c20c55474d3b50
2 changes: 1 addition & 1 deletion rust/macros/concat_idents.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0

use proc_macro::{token_stream, Ident, TokenStream, TokenTree};
use proc_macro2::{token_stream, Ident, TokenStream, TokenTree};

use crate::helpers::expect_punct;

Expand Down
4 changes: 2 additions & 2 deletions rust/macros/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0

use proc_macro::{token_stream, Group, TokenStream, TokenTree};
use proc_macro2::{token_stream, Group, TokenStream, TokenTree};

pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
if let Some(TokenTree::Ident(ident)) = it.next() {
Expand Down Expand Up @@ -166,7 +166,7 @@ pub(crate) fn parse_generics(input: TokenStream) -> (Generics, Vec<TokenTree>) {
1 => {
// Here depending on the token, it might be a generic variable name.
match tt.clone() {
TokenTree::Ident(i) if at_start && i.to_string() == "const" => {
TokenTree::Ident(i) if at_start && i == "const" => {
let Some(name) = toks.next() else {
// Parsing error.
break;
Expand Down
20 changes: 11 additions & 9 deletions rust/macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

//! Crate for all kernel procedural macros.

#[macro_use]
mod quote;
mod concat_idents;
mod helpers;
mod module;
Expand Down Expand Up @@ -76,7 +74,7 @@ use proc_macro::TokenStream;
/// - `alias`: byte array of alias name of the kernel module.
#[proc_macro]
pub fn module(ts: TokenStream) -> TokenStream {
module::module(ts)
module::module(ts.into()).into()
}

/// Declares or implements a vtable trait.
Expand Down Expand Up @@ -151,7 +149,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
/// [`kernel::error::VTABLE_DEFAULT_ERROR`]: ../kernel/error/constant.VTABLE_DEFAULT_ERROR.html
#[proc_macro_attribute]
pub fn vtable(attr: TokenStream, ts: TokenStream) -> TokenStream {
vtable::vtable(attr, ts)
vtable::vtable(attr.into(), ts.into()).into()
}

/// Concatenate two identifiers.
Expand Down Expand Up @@ -194,7 +192,7 @@ pub fn vtable(attr: TokenStream, ts: TokenStream) -> TokenStream {
/// ```
#[proc_macro]
pub fn concat_idents(ts: TokenStream) -> TokenStream {
concat_idents::concat_idents(ts)
concat_idents::concat_idents(ts.into()).into()
}

/// Used to specify the pinning information of the fields of a struct.
Expand Down Expand Up @@ -243,7 +241,7 @@ pub fn concat_idents(ts: TokenStream) -> TokenStream {
// ^ cannot use direct link, since `kernel` is not a dependency of `macros`.
#[proc_macro_attribute]
pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
pin_data::pin_data(inner, item)
pin_data::pin_data(inner.into(), item.into()).into()
}

/// Used to implement `PinnedDrop` safely.
Expand All @@ -270,7 +268,7 @@ pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
/// ```
#[proc_macro_attribute]
pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
pinned_drop::pinned_drop(args, input)
pinned_drop::pinned_drop(args.into(), input.into()).into()
}

/// Paste identifiers together.
Expand Down Expand Up @@ -382,9 +380,13 @@ pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
/// [`paste`]: https://docs.rs/paste/
#[proc_macro]
pub fn paste(input: TokenStream) -> TokenStream {
let input: proc_macro2::TokenStream = input.into();
let mut tokens = input.into_iter().collect();
paste::expand(&mut tokens);
tokens.into_iter().collect()
tokens
.into_iter()
.collect::<proc_macro2::TokenStream>()
.into()
}

/// Derives the [`Zeroable`] trait for the given struct.
Expand All @@ -403,5 +405,5 @@ pub fn paste(input: TokenStream) -> TokenStream {
/// ```
#[proc_macro_derive(Zeroable)]
pub fn derive_zeroable(input: TokenStream) -> TokenStream {
zeroable::derive(input)
zeroable::derive(input.into()).into()
}
2 changes: 1 addition & 1 deletion rust/macros/module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0

use crate::helpers::*;
use proc_macro::{token_stream, Delimiter, Literal, TokenStream, TokenTree};
use proc_macro2::{token_stream, Delimiter, Literal, TokenStream, TokenTree};
use std::fmt::Write;

fn expect_string_array(it: &mut token_stream::IntoIter) -> Vec<String> {
Expand Down
2 changes: 1 addition & 1 deletion rust/macros/paste.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0

use proc_macro::{Delimiter, Group, Ident, Spacing, Span, TokenTree};
use proc_macro2::{Delimiter, Group, Ident, Spacing, Span, TokenTree};

fn concat(tokens: &[TokenTree], group_span: Span) -> TokenTree {
let mut tokens = tokens.iter();
Expand Down
15 changes: 6 additions & 9 deletions rust/macros/pin_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::helpers::{parse_generics, Generics};
use proc_macro::{Group, Punct, Spacing, TokenStream, TokenTree};
use proc_macro2::{Group, Punct, Spacing, TokenStream, TokenTree};
use quote::quote;

pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
// This proc-macro only does some pre-parsing and then delegates the actual parsing to
Expand All @@ -25,7 +26,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
// The name of the struct with ty_generics.
let struct_name = rest
.iter()
.skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if i.to_string() == "struct"))
.skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if *i == "struct"))
.nth(1)
.and_then(|tt| match tt {
TokenTree::Ident(_) => {
Expand Down Expand Up @@ -62,7 +63,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
.into_iter()
.flat_map(|tt| {
// We ignore top level `struct` tokens, since they would emit a compile error.
if matches!(&tt, TokenTree::Ident(i) if i.to_string() == "struct") {
if matches!(&tt, TokenTree::Ident(i) if *i == "struct") {
vec![tt]
} else {
replace_self_and_deny_type_defs(&struct_name, tt, &mut errs)
Expand Down Expand Up @@ -95,11 +96,7 @@ fn replace_self_and_deny_type_defs(
) -> Vec<TokenTree> {
match tt {
TokenTree::Ident(ref i)
if i.to_string() == "enum"
|| i.to_string() == "trait"
|| i.to_string() == "struct"
|| i.to_string() == "union"
|| i.to_string() == "impl" =>
if *i == "enum" || *i == "trait" || *i == "struct" || *i == "union" || *i == "impl" =>
{
errs.extend(
format!(
Expand All @@ -116,7 +113,7 @@ fn replace_self_and_deny_type_defs(
);
vec![tt]
}
TokenTree::Ident(i) if i.to_string() == "Self" => struct_name.clone(),
TokenTree::Ident(i) if i == "Self" => struct_name.clone(),
TokenTree::Literal(_) | TokenTree::Punct(_) | TokenTree::Ident(_) => vec![tt],
TokenTree::Group(g) => vec![TokenTree::Group(Group::new(
g.delimiter(),
Expand Down
7 changes: 4 additions & 3 deletions rust/macros/pinned_drop.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

use proc_macro::{TokenStream, TokenTree};
use proc_macro2::{TokenStream, TokenTree};
use quote::quote;

pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream {
let mut toks = input.into_iter().collect::<Vec<_>>();
assert!(!toks.is_empty());
// Ensure that we have an `impl` item.
assert!(matches!(&toks[0], TokenTree::Ident(i) if i.to_string() == "impl"));
assert!(matches!(&toks[0], TokenTree::Ident(i) if *i == "impl"));
// Ensure that we are implementing `PinnedDrop`.
let mut nesting: usize = 0;
let mut pinned_drop_idx = None;
Expand All @@ -24,7 +25,7 @@ pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream
if i >= 1 && nesting == 0 {
// Found the end of the generics, this should be `PinnedDrop`.
assert!(
matches!(tt, TokenTree::Ident(i) if i.to_string() == "PinnedDrop"),
matches!(tt, TokenTree::Ident(i) if *i == "PinnedDrop"),
"expected 'PinnedDrop', found: '{:?}'",
tt
);
Expand Down
157 changes: 0 additions & 157 deletions rust/macros/quote.rs

This file was deleted.

6 changes: 3 additions & 3 deletions rust/macros/vtable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0

use proc_macro::{Delimiter, Group, TokenStream, TokenTree};
use proc_macro2::{Delimiter, Group, TokenStream, TokenTree};
use std::collections::HashSet;
use std::fmt::Write;

Expand Down Expand Up @@ -31,15 +31,15 @@ pub(crate) fn vtable(_attr: TokenStream, ts: TokenStream) -> TokenStream {
let mut consts = HashSet::new();
while let Some(token) = body_it.next() {
match token {
TokenTree::Ident(ident) if ident.to_string() == "fn" => {
TokenTree::Ident(ident) if ident == "fn" => {
let fn_name = match body_it.next() {
Some(TokenTree::Ident(ident)) => ident.to_string(),
// Possibly we've encountered a fn pointer type instead.
_ => continue,
};
functions.push(fn_name);
}
TokenTree::Ident(ident) if ident.to_string() == "const" => {
TokenTree::Ident(ident) if ident == "const" => {
let const_name = match body_it.next() {
Some(TokenTree::Ident(ident)) => ident.to_string(),
// Possibly we've encountered an inline const block instead.
Expand Down
3 changes: 2 additions & 1 deletion rust/macros/zeroable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: GPL-2.0

use crate::helpers::{parse_generics, Generics};
use proc_macro::{TokenStream, TokenTree};
use proc_macro2::{TokenStream, TokenTree};
use quote::quote;

pub(crate) fn derive(input: TokenStream) -> TokenStream {
let (
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy