Skip to content

Commit 185dd4a

Browse files
committed
Split derive-impl and derive
1 parent 38a735a commit 185dd4a

15 files changed

+148
-97
lines changed

Diff for: Cargo.lock

+9-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ include = ["LICENSE", "Cargo.toml", "src/**/*.rs"]
1515
resolver = "2"
1616
members = [
1717
"compiler", "compiler/ast", "compiler/core", "compiler/codegen", "compiler/parser",
18-
".", "common", "derive", "jit", "vm", "pylib", "stdlib", "wasm/lib",
18+
".", "common", "derive", "jit", "vm", "pylib", "stdlib", "wasm/lib", "derive-impl",
1919
]
2020

2121
[features]

Diff for: compiler/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use rustpython_codegen::{compile, symboltable};
2-
use rustpython_compiler_core::CodeObject;
32
use rustpython_parser::{
43
ast::{fold::Fold, ConstantOptimizer},
54
error::ParseErrorType,
65
parser,
76
};
87

98
pub use rustpython_codegen::compile::CompileOpts;
9+
pub use rustpython_compiler_core::CodeObject;
1010
pub use rustpython_compiler_core::{BaseError as CompileErrorBody, Mode};
1111

1212
#[derive(Debug, thiserror::Error)]

Diff for: derive-impl/Cargo.toml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "rustpython-derive-impl"
3+
version = "0.0.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rustpython-compiler-core = { path = "../compiler/core", version = "0.1.1" }
8+
rustpython-doc = { git = "https://github.com/RustPython/__doc__", branch = "main" }
9+
10+
indexmap = "1.8.1"
11+
itertools = "0.10.3"
12+
maplit = "1.0.2"
13+
once_cell = "1.10.0"
14+
proc-macro2 = "1.0.37"
15+
quote = "1.0.18"
16+
syn = { version = "1.0.91", features = ["full", "extra-traits"] }
17+
syn-ext = { version = "0.4.0", features = ["full"] }
18+
textwrap = { version = "0.15.0", default-features = false }
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: derive-impl/src/lib.rs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#![recursion_limit = "128"]
2+
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
3+
#![doc(html_root_url = "https://docs.rs/rustpython-derive/")]
4+
5+
extern crate proc_macro;
6+
7+
#[macro_use]
8+
extern crate maplit;
9+
10+
#[macro_use]
11+
mod error;
12+
#[macro_use]
13+
mod util;
14+
15+
mod compile_bytecode;
16+
mod from_args;
17+
mod pyclass;
18+
mod pymodule;
19+
mod pypayload;
20+
mod pystructseq;
21+
22+
use error::{extract_spans, Diagnostic};
23+
use proc_macro2::TokenStream;
24+
use quote::ToTokens;
25+
use rustpython_doc as doc;
26+
use syn::{AttributeArgs, DeriveInput, Item};
27+
28+
pub use compile_bytecode::Compiler;
29+
30+
fn result_to_tokens(result: Result<TokenStream, impl Into<Diagnostic>>) -> TokenStream {
31+
result
32+
.map_err(|e| e.into())
33+
.unwrap_or_else(ToTokens::into_token_stream)
34+
}
35+
36+
pub fn derive_from_args(input: DeriveInput) -> TokenStream {
37+
result_to_tokens(from_args::impl_from_args(input))
38+
}
39+
40+
pub fn pyclass(attr: AttributeArgs, item: Item) -> TokenStream {
41+
if matches!(item, syn::Item::Impl(_) | syn::Item::Trait(_)) {
42+
result_to_tokens(pyclass::impl_pyimpl(attr, item))
43+
} else {
44+
result_to_tokens(pyclass::impl_pyclass(attr, item))
45+
}
46+
}
47+
48+
pub use pyclass::PyExceptionDef;
49+
pub fn define_exception(exc_def: PyExceptionDef) -> TokenStream {
50+
result_to_tokens(pyclass::impl_define_exception(exc_def))
51+
}
52+
53+
pub fn pyexception(attr: AttributeArgs, item: Item) -> TokenStream {
54+
result_to_tokens(pyclass::impl_pyexception(attr, item))
55+
}
56+
57+
pub fn pymodule(attr: AttributeArgs, item: Item) -> TokenStream {
58+
result_to_tokens(pymodule::impl_pymodule(attr, item))
59+
}
60+
61+
pub fn pystruct_sequence(input: DeriveInput) -> TokenStream {
62+
result_to_tokens(pystructseq::impl_pystruct_sequence(input))
63+
}
64+
65+
pub fn pystruct_sequence_try_from_object(input: DeriveInput) -> TokenStream {
66+
result_to_tokens(pystructseq::impl_pystruct_sequence_try_from_object(input))
67+
}
68+
69+
pub fn py_compile(input: TokenStream, compiler: &dyn Compiler) -> TokenStream {
70+
result_to_tokens(compile_bytecode::impl_py_compile(input, compiler))
71+
}
72+
73+
pub fn py_freeze(input: TokenStream, compiler: &dyn Compiler) -> TokenStream {
74+
result_to_tokens(compile_bytecode::impl_py_freeze(input, compiler))
75+
}
76+
77+
pub fn pypayload(input: DeriveInput) -> TokenStream {
78+
result_to_tokens(pypayload::impl_pypayload(input))
79+
}

Diff for: derive/src/pyclass.rs renamed to derive-impl/src/pyclass.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ where
12841284
}
12851285

12861286
#[derive(Debug)]
1287-
pub(crate) struct PyExceptionDef {
1287+
pub struct PyExceptionDef {
12881288
pub class_name: Ident,
12891289
pub base_class: Ident,
12901290
pub ctx_name: Ident,
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: derive/Cargo.toml

+2-12
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,5 @@ proc-macro = true
1212

1313
[dependencies]
1414
rustpython-compiler = { path = "../compiler", version = "0.1.1" }
15-
rustpython-compiler-core = { path = "../compiler/core", version = "0.1.1" }
16-
rustpython-doc = { git = "https://github.com/RustPython/__doc__", branch = "main" }
17-
18-
indexmap = "1.8.1"
19-
itertools = "0.10.3"
20-
maplit = "1.0.2"
21-
once_cell = "1.10.0"
22-
proc-macro2 = "1.0.37"
23-
quote = "1.0.18"
24-
syn = { version = "1.0.91", features = ["full", "extra-traits"] }
25-
syn-ext = { version = "0.4.0", features = ["full"] }
26-
textwrap = { version = "0.15.0", default-features = false }
15+
rustpython-derive-impl = { path = "../derive-impl" }
16+
syn = "1.0.91"

Diff for: derive/src/lib.rs

+37-81
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,21 @@
22
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
33
#![doc(html_root_url = "https://docs.rs/rustpython-derive/")]
44

5-
extern crate proc_macro;
6-
7-
#[macro_use]
8-
extern crate maplit;
9-
10-
#[macro_use]
11-
mod error;
12-
#[macro_use]
13-
mod util;
14-
15-
mod compile_bytecode;
16-
mod from_args;
17-
mod pyclass;
18-
mod pymodule;
19-
mod pypayload;
20-
mod pystructseq;
21-
22-
use error::{extract_spans, Diagnostic};
23-
use proc_macro2::TokenStream;
24-
use quote::ToTokens;
25-
use rustpython_doc as doc;
26-
use syn::{parse_macro_input, AttributeArgs, DeriveInput, Item};
27-
28-
fn result_to_tokens(result: Result<TokenStream, impl Into<Diagnostic>>) -> proc_macro::TokenStream {
29-
result
30-
.map_err(|e| e.into())
31-
.unwrap_or_else(ToTokens::into_token_stream)
32-
.into()
33-
}
5+
use proc_macro::TokenStream;
6+
use rustpython_derive_impl as derive_impl;
7+
use syn::parse_macro_input;
348

359
#[proc_macro_derive(FromArgs, attributes(pyarg))]
36-
pub fn derive_from_args(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
37-
let input = parse_macro_input!(input as DeriveInput);
38-
result_to_tokens(from_args::impl_from_args(input))
10+
pub fn derive_from_args(input: TokenStream) -> TokenStream {
11+
let input = parse_macro_input!(input);
12+
derive_impl::derive_from_args(input).into()
3913
}
4014

4115
#[proc_macro_attribute]
42-
pub fn pyclass(
43-
attr: proc_macro::TokenStream,
44-
item: proc_macro::TokenStream,
45-
) -> proc_macro::TokenStream {
46-
let attr = parse_macro_input!(attr as AttributeArgs);
47-
let item = parse_macro_input!(item as Item);
48-
if matches!(item, syn::Item::Impl(_) | syn::Item::Trait(_)) {
49-
result_to_tokens(pyclass::impl_pyimpl(attr, item))
50-
} else {
51-
result_to_tokens(pyclass::impl_pyclass(attr, item))
52-
}
16+
pub fn pyclass(attr: TokenStream, item: TokenStream) -> TokenStream {
17+
let attr = parse_macro_input!(attr);
18+
let item = parse_macro_input!(item);
19+
derive_impl::pyclass(attr, item).into()
5320
}
5421

5522
/// This macro serves a goal of generating multiple
@@ -63,75 +30,64 @@ pub fn pyclass(
6330
/// So, we use `extend_class!` macro as the second
6431
/// step in exception type definition.
6532
#[proc_macro]
66-
pub fn define_exception(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
67-
let exc_def = parse_macro_input!(input as pyclass::PyExceptionDef);
68-
result_to_tokens(pyclass::impl_define_exception(exc_def))
33+
pub fn define_exception(input: TokenStream) -> TokenStream {
34+
let input = parse_macro_input!(input);
35+
derive_impl::define_exception(input).into()
6936
}
7037

7138
/// Helper macro to define `Exception` types.
7239
/// More-or-less is an alias to `pyclass` macro.
7340
#[proc_macro_attribute]
74-
pub fn pyexception(
75-
attr: proc_macro::TokenStream,
76-
item: proc_macro::TokenStream,
77-
) -> proc_macro::TokenStream {
78-
let attr = parse_macro_input!(attr as AttributeArgs);
79-
let item = parse_macro_input!(item as Item);
80-
result_to_tokens(pyclass::impl_pyexception(attr, item))
41+
pub fn pyexception(attr: TokenStream, item: TokenStream) -> TokenStream {
42+
let attr = parse_macro_input!(attr);
43+
let item = parse_macro_input!(item);
44+
derive_impl::pyexception(attr, item).into()
8145
}
8246

8347
#[proc_macro_attribute]
84-
pub fn pymodule(
85-
attr: proc_macro::TokenStream,
86-
item: proc_macro::TokenStream,
87-
) -> proc_macro::TokenStream {
88-
let attr = parse_macro_input!(attr as AttributeArgs);
89-
let item = parse_macro_input!(item as Item);
90-
result_to_tokens(pymodule::impl_pymodule(attr, item))
48+
pub fn pymodule(attr: TokenStream, item: TokenStream) -> TokenStream {
49+
let attr = parse_macro_input!(attr);
50+
let item = parse_macro_input!(item);
51+
derive_impl::pymodule(attr, item).into()
9152
}
9253

9354
#[proc_macro_derive(PyStructSequence)]
94-
pub fn pystruct_sequence(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
95-
let input = parse_macro_input!(input as DeriveInput);
96-
result_to_tokens(pystructseq::impl_pystruct_sequence(input))
55+
pub fn pystruct_sequence(input: TokenStream) -> TokenStream {
56+
let input = parse_macro_input!(input);
57+
derive_impl::pystruct_sequence(input).into()
9758
}
9859

9960
#[proc_macro_derive(TryIntoPyStructSequence)]
100-
pub fn pystruct_sequence_try_from_object(
101-
input: proc_macro::TokenStream,
102-
) -> proc_macro::TokenStream {
103-
let input = parse_macro_input!(input as DeriveInput);
104-
result_to_tokens(pystructseq::impl_pystruct_sequence_try_from_object(input))
61+
pub fn pystruct_sequence_try_from_object(input: TokenStream) -> TokenStream {
62+
let input = parse_macro_input!(input);
63+
derive_impl::pystruct_sequence_try_from_object(input).into()
10564
}
10665

107-
// would be cool to move all the macro implementation to a separate rustpython-derive-shared
108-
// that just depends on rustpython-compiler-core, and then rustpython-derive would hook -compiler
109-
// up to it; so that (the bulk of) rustpython-derive and rustpython-codegen could build in parallel
11066
struct Compiler;
111-
impl compile_bytecode::Compiler for Compiler {
67+
impl derive_impl::Compiler for Compiler {
11268
fn compile(
11369
&self,
11470
source: &str,
115-
mode: rustpython_compiler_core::Mode,
71+
mode: rustpython_compiler::Mode,
11672
module_name: String,
117-
) -> Result<rustpython_compiler_core::CodeObject, Box<dyn std::error::Error>> {
73+
) -> Result<rustpython_compiler::CodeObject, Box<dyn std::error::Error>> {
11874
use rustpython_compiler::{compile, CompileOpts};
11975
Ok(compile(source, mode, module_name, CompileOpts::default())?)
12076
}
12177
}
12278

12379
#[proc_macro]
124-
pub fn py_compile(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
125-
result_to_tokens(compile_bytecode::impl_py_compile(input.into(), &Compiler))
80+
pub fn py_compile(input: TokenStream) -> TokenStream {
81+
derive_impl::py_compile(input.into(), &Compiler).into()
12682
}
12783

12884
#[proc_macro]
129-
pub fn py_freeze(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
130-
result_to_tokens(compile_bytecode::impl_py_freeze(input.into(), &Compiler))
85+
pub fn py_freeze(input: TokenStream) -> TokenStream {
86+
derive_impl::py_freeze(input.into(), &Compiler).into()
13187
}
13288

13389
#[proc_macro_derive(PyPayload)]
134-
pub fn pypayload(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
135-
let input = parse_macro_input!(input as DeriveInput);
136-
result_to_tokens(pypayload::impl_pypayload(input))
90+
pub fn pypayload(input: TokenStream) -> TokenStream {
91+
let input = parse_macro_input!(input);
92+
derive_impl::pypayload(input).into()
13793
}

0 commit comments

Comments
 (0)