pyforge-macros-backend 0.3.0

Code generation backend for PyForge macros
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Generates introspection data i.e. JSON strings in the .pyo3i0 section.
//!
//! There is a JSON per PyForge proc macro (pyclass, pymodule, pyfunction...).
//!
//! These JSON blobs can refer to each others via the _PYO3_INTROSPECTION_ID constants
//! providing unique ids for each element.
//!
//! The JSON blobs format must be synchronized with the `pyo3_introspection::introspection.rs::Chunk`
//! type that is used to parse them.

use crate::method::{FnArg, RegularArg};
use crate::py_expr::PyExpr;
use crate::pyfunction::FunctionSignature;
use crate::utils::{PyForgeCratePath, PythonDoc, StrOrExpr};
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, ToTokens};
use std::borrow::Cow;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::fmt::Write;
use std::hash::{Hash, Hasher};
use std::mem::take;
use std::sync::atomic::{AtomicUsize, Ordering};
use syn::{Attribute, Ident, ReturnType, Type, TypePath};

static GLOBAL_COUNTER_FOR_UNIQUE_NAMES: AtomicUsize = AtomicUsize::new(0);

pub fn module_introspection_code<'a>(
    pyo3_crate_path: &PyForgeCratePath,
    name: &str,
    members: impl IntoIterator<Item = &'a Ident>,
    members_cfg_attrs: impl IntoIterator<Item = &'a Vec<Attribute>>,
    doc: Option<&PythonDoc>,
    incomplete: bool,
) -> TokenStream {
    let mut desc = HashMap::from([
        ("type", IntrospectionNode::String("module".into())),
        ("id", IntrospectionNode::IntrospectionId(None)),
        ("name", IntrospectionNode::String(name.into())),
        (
            "members",
            IntrospectionNode::List(
                members
                    .into_iter()
                    .zip(members_cfg_attrs)
                    .map(|(member, attributes)| AttributedIntrospectionNode {
                        node: IntrospectionNode::IntrospectionId(Some(ident_to_type(member))),
                        attributes,
                    })
                    .collect(),
            ),
        ),
        ("incomplete", IntrospectionNode::Bool(incomplete)),
    ]);
    if let Some(doc) = doc {
        desc.insert("doc", IntrospectionNode::Doc(doc));
    }
    IntrospectionNode::Map(desc).emit(pyo3_crate_path)
}

pub fn class_introspection_code(
    pyo3_crate_path: &PyForgeCratePath,
    ident: &Ident,
    name: &str,
    extends: Option<PyExpr>,
    is_final: bool,
    parent: Option<&Type>,
    doc: Option<&PythonDoc>,
) -> TokenStream {
    let mut desc = HashMap::from([
        ("type", IntrospectionNode::String("class".into())),
        (
            "id",
            IntrospectionNode::IntrospectionId(Some(ident_to_type(ident))),
        ),
        ("name", IntrospectionNode::String(name.into())),
    ]);
    if let Some(extends) = extends {
        desc.insert("bases", IntrospectionNode::List(vec![extends.into()]));
    }
    if is_final {
        desc.insert(
            "decorators",
            IntrospectionNode::List(vec![PyExpr::module_attr("typing", "final").into()]),
        );
    }
    if let Some(parent) = parent {
        desc.insert(
            "parent",
            IntrospectionNode::IntrospectionId(Some(Cow::Borrowed(parent))),
        );
    }
    if let Some(doc) = &doc {
        desc.insert("doc", IntrospectionNode::Doc(doc));
    }
    IntrospectionNode::Map(desc).emit(pyo3_crate_path)
}

#[expect(clippy::too_many_arguments)]
pub fn function_introspection_code(
    pyo3_crate_path: &PyForgeCratePath,
    ident: Option<&Ident>,
    name: &str,
    signature: &FunctionSignature<'_>,
    first_argument: Option<&'static str>,
    returns: ReturnType,
    decorators: impl IntoIterator<Item = PyExpr>,
    is_async: bool,
    is_returning_not_implemented_on_extraction_error: bool,
    doc: Option<&PythonDoc>,
    parent: Option<&Type>,
) -> TokenStream {
    let mut desc = HashMap::from([
        ("type", IntrospectionNode::String("function".into())),
        ("name", IntrospectionNode::String(name.into())),
        (
            "arguments",
            arguments_introspection_data(
                signature,
                first_argument,
                is_returning_not_implemented_on_extraction_error,
                parent,
            ),
        ),
        (
            "returns",
            if let Some((_, returns)) = signature
                .attribute
                .as_ref()
                .and_then(|attribute| attribute.value.returns.as_ref())
            {
                returns.as_type_hint().into()
            } else {
                match returns {
                    ReturnType::Default => PyExpr::builtin("None"),
                    ReturnType::Type(_, ty) => PyExpr::from_return_type(*ty, parent),
                }
                .into()
            },
        ),
    ]);
    if is_async {
        desc.insert("async", IntrospectionNode::Bool(true));
    }
    if let Some(ident) = ident {
        desc.insert(
            "id",
            IntrospectionNode::IntrospectionId(Some(ident_to_type(ident))),
        );
    }
    let decorators = decorators.into_iter().map(|d| d.into()).collect::<Vec<_>>();
    if !decorators.is_empty() {
        desc.insert("decorators", IntrospectionNode::List(decorators));
    }
    if let Some(doc) = doc {
        desc.insert("doc", IntrospectionNode::Doc(doc));
    }
    if let Some(parent) = parent {
        desc.insert(
            "parent",
            IntrospectionNode::IntrospectionId(Some(Cow::Borrowed(parent))),
        );
    }
    IntrospectionNode::Map(desc).emit(pyo3_crate_path)
}

pub fn attribute_introspection_code(
    pyo3_crate_path: &PyForgeCratePath,
    parent: Option<&Type>,
    name: String,
    value: PyExpr,
    rust_type: Type,
    doc: Option<&PythonDoc>,
    is_final: bool,
) -> TokenStream {
    let mut desc = HashMap::from([
        ("type", IntrospectionNode::String("attribute".into())),
        ("name", IntrospectionNode::String(name.into())),
        (
            "parent",
            IntrospectionNode::IntrospectionId(parent.map(Cow::Borrowed)),
        ),
    ]);
    if value == PyExpr::ellipsis() {
        // We need to set a type, but not need to set the value to ..., all attributes have a value
        desc.insert(
            "annotation",
            if is_final {
                PyExpr::subscript(
                    PyExpr::module_attr("typing", "Final"),
                    PyExpr::from_return_type(rust_type, parent),
                )
                .into()
            } else {
                PyExpr::from_return_type(rust_type, parent).into()
            },
        );
    } else {
        desc.insert(
            "annotation",
            if is_final {
                // Type checkers can infer the type from the value because it's typing.Literal[value]
                // So, following stubs best practices, we only write typing.Final and not
                // typing.Final[typing.literal[value]]
                PyExpr::module_attr("typing", "Final")
            } else {
                PyExpr::from_return_type(rust_type, parent)
            }
            .into(),
        );
        desc.insert("value", value.into());
    }
    if let Some(doc) = doc {
        desc.insert("doc", IntrospectionNode::Doc(doc));
    }
    IntrospectionNode::Map(desc).emit(pyo3_crate_path)
}

fn arguments_introspection_data<'a>(
    signature: &'a FunctionSignature<'a>,
    first_argument: Option<&'a str>,
    is_returning_not_implemented_on_extraction_error: bool,
    class_type: Option<&Type>,
) -> IntrospectionNode<'a> {
    let mut argument_desc = signature.arguments.iter().filter(|arg| {
        matches!(
            arg,
            FnArg::Regular(_) | FnArg::VarArgs(_) | FnArg::KwArgs(_)
        )
    });

    let mut posonlyargs = Vec::new();
    let mut args = Vec::new();
    let mut vararg = None;
    let mut kwonlyargs = Vec::new();
    let mut kwarg = None;

    if let Some(first_argument) = first_argument {
        posonlyargs.push(
            IntrospectionNode::Map(
                [("name", IntrospectionNode::String(first_argument.into()))].into(),
            )
            .into(),
        );
    }

    for (i, param) in signature
        .python_signature
        .positional_parameters
        .iter()
        .enumerate()
    {
        let arg_desc = if let Some(FnArg::Regular(arg_desc)) = argument_desc.next() {
            arg_desc
        } else {
            panic!("Less arguments than in python signature");
        };
        let arg = argument_introspection_data(
            param,
            arg_desc,
            is_returning_not_implemented_on_extraction_error,
            class_type,
        );
        if i < signature.python_signature.positional_only_parameters {
            posonlyargs.push(arg);
        } else {
            args.push(arg)
        }
    }

    if let Some(param) = &signature.python_signature.varargs {
        let Some(FnArg::VarArgs(arg_desc)) = argument_desc.next() else {
            panic!("Fewer arguments than in python signature");
        };
        let mut params = HashMap::from([("name", IntrospectionNode::String(param.into()))]);
        if let Some(annotation) = &arg_desc.annotation {
            params.insert("annotation", annotation.clone().into());
        }
        vararg = Some(IntrospectionNode::Map(params));
    }

    for (param, _) in &signature.python_signature.keyword_only_parameters {
        let Some(FnArg::Regular(arg_desc)) = argument_desc.next() else {
            panic!("Less arguments than in python signature");
        };
        kwonlyargs.push(argument_introspection_data(
            param,
            arg_desc,
            is_returning_not_implemented_on_extraction_error,
            class_type,
        ));
    }

    if let Some(param) = &signature.python_signature.kwargs {
        let Some(FnArg::KwArgs(arg_desc)) = argument_desc.next() else {
            panic!("Less arguments than in python signature");
        };
        let mut params = HashMap::from([("name", IntrospectionNode::String(param.into()))]);
        if let Some(annotation) = &arg_desc.annotation {
            params.insert("annotation", annotation.clone().into());
        }
        kwarg = Some(IntrospectionNode::Map(params));
    }

    let mut map = HashMap::new();
    if !posonlyargs.is_empty() {
        map.insert("posonlyargs", IntrospectionNode::List(posonlyargs));
    }
    if !args.is_empty() {
        map.insert("args", IntrospectionNode::List(args));
    }
    if let Some(vararg) = vararg {
        map.insert("vararg", vararg);
    }
    if !kwonlyargs.is_empty() {
        map.insert("kwonlyargs", IntrospectionNode::List(kwonlyargs));
    }
    if let Some(kwarg) = kwarg {
        map.insert("kwarg", kwarg);
    }
    IntrospectionNode::Map(map)
}

fn argument_introspection_data<'a>(
    name: &'a str,
    desc: &'a RegularArg<'_>,
    is_returning_not_implemented_on_extraction_error: bool,
    class_type: Option<&Type>,
) -> AttributedIntrospectionNode<'a> {
    let mut params: HashMap<_, _> = [("name", IntrospectionNode::String(name.into()))].into();
    if let Some(expr) = &desc.default_value {
        params.insert("default", PyExpr::constant_from_expression(expr).into());
    }

    if is_returning_not_implemented_on_extraction_error {
        // all inputs are allowed, we use `object`
        params.insert("annotation", PyExpr::builtin("object").into());
    } else if let Some(annotation) = &desc.annotation {
        params.insert("annotation", annotation.clone().into());
    } else if desc.from_py_with.is_none() {
        // If from_py_with is set we don't know anything on the input type
        params.insert(
            "annotation",
            PyExpr::from_argument_type(desc.ty.clone(), class_type).into(),
        );
    }
    IntrospectionNode::Map(params).into()
}

enum IntrospectionNode<'a> {
    String(Cow<'a, str>),
    Bool(bool),
    IntrospectionId(Option<Cow<'a, Type>>),
    TypeHint(Cow<'a, PyExpr>),
    Doc(&'a PythonDoc),
    Map(HashMap<&'static str, IntrospectionNode<'a>>),
    List(Vec<AttributedIntrospectionNode<'a>>),
}

impl IntrospectionNode<'_> {
    fn emit(self, pyo3_crate_path: &PyForgeCratePath) -> TokenStream {
        let mut content = ConcatenationBuilder::default();
        self.add_to_serialization(&mut content, pyo3_crate_path);
        content.into_static(
            pyo3_crate_path,
            format_ident!("PYO3_INTROSPECTION_1_{}", unique_element_id()),
        )
    }

    fn add_to_serialization(
        self,
        content: &mut ConcatenationBuilder,
        pyo3_crate_path: &PyForgeCratePath,
    ) {
        match self {
            Self::String(string) => {
                content.push_str_to_escape(&string);
            }
            Self::Bool(value) => content.push_str(if value { "true" } else { "false" }),
            Self::IntrospectionId(ident) => {
                content.push_str("\"");
                content.push_tokens(if let Some(ident) = ident {
                    quote! { #ident::_PYO3_INTROSPECTION_ID.as_bytes() }
                } else {
                    quote! { _PYO3_INTROSPECTION_ID.as_bytes() }
                });
                content.push_str("\"");
            }
            Self::TypeHint(hint) => {
                content.push_tokens(serialize_type_hint(
                    hint.to_introspection_token_stream(pyo3_crate_path),
                    pyo3_crate_path,
                ));
            }
            Self::Doc(doc) => {
                content.push_str("\"");
                for part in &doc.parts {
                    match part {
                        StrOrExpr::Str {value, ..} => content.push_str(&escape_json_string(value)),
                        StrOrExpr::Expr(value) => content.push_tokens(quote! {{
                            const DOC: &str = #value;
                            const DOC_LEN: usize = #pyo3_crate_path::impl_::introspection::escaped_json_string_len(&DOC);
                            const DOC_SER: [u8; DOC_LEN] = {
                                let mut result: [u8; DOC_LEN] = [0; DOC_LEN];
                                #pyo3_crate_path::impl_::introspection::escape_json_string(&DOC, &mut result);
                                result
                            };
                            &DOC_SER
                        }}),
                    }
                }
                content.push_str("\"");
            }
            Self::Map(map) => {
                content.push_str("{");
                for (i, (key, value)) in map.into_iter().enumerate() {
                    if i > 0 {
                        content.push_str(",");
                    }
                    content.push_str_to_escape(key);
                    content.push_str(":");
                    value.add_to_serialization(content, pyo3_crate_path);
                }
                content.push_str("}");
            }
            Self::List(list) => {
                content.push_str("[");
                for (i, AttributedIntrospectionNode { node, attributes }) in
                    list.into_iter().enumerate()
                {
                    if attributes.is_empty() {
                        if i > 0 {
                            content.push_str(",");
                        }
                        node.add_to_serialization(content, pyo3_crate_path);
                    } else {
                        // We serialize the element to easily gate it behind the attributes
                        let mut nested_builder = ConcatenationBuilder::default();
                        if i > 0 {
                            nested_builder.push_str(",");
                        }
                        node.add_to_serialization(&mut nested_builder, pyo3_crate_path);
                        let nested_content = nested_builder.into_token_stream(pyo3_crate_path);
                        content.push_tokens(quote! { #(#attributes)* #nested_content });
                    }
                }
                content.push_str("]");
            }
        }
    }
}

impl From<PyExpr> for IntrospectionNode<'static> {
    fn from(element: PyExpr) -> Self {
        Self::TypeHint(Cow::Owned(element))
    }
}

fn serialize_type_hint(hint: TokenStream, pyo3_crate_path: &PyForgeCratePath) -> TokenStream {
    quote! {{
        const TYPE_HINT: #pyo3_crate_path::inspect::PyStaticExpr = #hint;
        const TYPE_HINT_LEN: usize = #pyo3_crate_path::inspect::serialized_len_for_introspection(&TYPE_HINT);
        const TYPE_HINT_SER: [u8; TYPE_HINT_LEN] = {
            let mut result: [u8; TYPE_HINT_LEN] = [0; TYPE_HINT_LEN];
            #pyo3_crate_path::inspect::serialize_for_introspection(&TYPE_HINT, &mut result);
            result
        };
        &TYPE_HINT_SER
    }}
}

struct AttributedIntrospectionNode<'a> {
    node: IntrospectionNode<'a>,
    attributes: &'a [Attribute],
}

impl<'a> From<IntrospectionNode<'a>> for AttributedIntrospectionNode<'a> {
    fn from(node: IntrospectionNode<'a>) -> Self {
        Self {
            node,
            attributes: &[],
        }
    }
}

impl<'a> From<PyExpr> for AttributedIntrospectionNode<'a> {
    fn from(node: PyExpr) -> Self {
        IntrospectionNode::from(node).into()
    }
}

#[derive(Default)]
pub struct ConcatenationBuilder {
    elements: Vec<ConcatenationBuilderElement>,
    current_string: String,
}

impl ConcatenationBuilder {
    pub fn push_tokens(&mut self, token_stream: TokenStream) {
        if !self.current_string.is_empty() {
            self.elements.push(ConcatenationBuilderElement::String(take(
                &mut self.current_string,
            )));
        }
        self.elements
            .push(ConcatenationBuilderElement::TokenStream(token_stream));
    }

    pub fn push_str(&mut self, value: &str) {
        self.current_string.push_str(value);
    }

    fn push_str_to_escape(&mut self, value: &str) {
        self.current_string.push('"');
        for c in value.chars() {
            match c {
                '\\' => self.current_string.push_str("\\\\"),
                '"' => self.current_string.push_str("\\\""),
                c => {
                    if c < char::from(32) {
                        panic!("ASCII chars below 32 are not allowed")
                    } else {
                        self.current_string.push(c);
                    }
                }
            }
        }
        self.current_string.push('"');
    }

    pub fn into_token_stream(self, pyo3_crate_path: &PyForgeCratePath) -> TokenStream {
        let mut elements = self.elements;
        if !self.current_string.is_empty() {
            elements.push(ConcatenationBuilderElement::String(self.current_string));
        }

        if let [ConcatenationBuilderElement::String(string)] = elements.as_slice() {
            // We avoid the const_concat! macro if there is only a single string
            return quote! { #string.as_bytes() };
        }

        quote! {
            {
                const PIECES: &[&[u8]] = &[#(#elements , )*];
                &#pyo3_crate_path::impl_::concat::combine_to_array::<{
                    #pyo3_crate_path::impl_::concat::combined_len(PIECES)
                }>(PIECES)
            }
        }
    }

    fn into_static(self, pyo3_crate_path: &PyForgeCratePath, ident: Ident) -> TokenStream {
        let mut elements = self.elements;
        if !self.current_string.is_empty() {
            elements.push(ConcatenationBuilderElement::String(self.current_string));
        }

        // #[no_mangle] is required to make sure some linkers like Linux ones do not mangle the section name too.
        quote! {
            const _: () = {
                const PIECES: &[&[u8]] = &[#(#elements , )*];
                const PIECES_LEN: usize = #pyo3_crate_path::impl_::concat::combined_len(PIECES);
                #[used]
                #[no_mangle]
                static #ident: #pyo3_crate_path::impl_::introspection::SerializedIntrospectionFragment<PIECES_LEN> = #pyo3_crate_path::impl_::introspection::SerializedIntrospectionFragment {
                    length: PIECES_LEN as u32,
                    fragment: #pyo3_crate_path::impl_::concat::combine_to_array::<PIECES_LEN>(PIECES)
                };
            };
        }
    }
}

enum ConcatenationBuilderElement {
    String(String),
    TokenStream(TokenStream),
}

impl ToTokens for ConcatenationBuilderElement {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            Self::String(s) => quote! { #s.as_bytes() }.to_tokens(tokens),
            Self::TokenStream(ts) => ts.to_tokens(tokens),
        }
    }
}

/// Generates a new unique identifier for linking introspection objects together
pub fn introspection_id_const() -> TokenStream {
    let id = unique_element_id().to_string();
    quote! {
        #[doc(hidden)]
        pub const _PYO3_INTROSPECTION_ID: &'static str = #id;
    }
}

pub fn unique_element_id() -> u64 {
    let mut hasher = DefaultHasher::new();
    format!("{:?}", Span::call_site()).hash(&mut hasher); // Distinguishes between call sites
    GLOBAL_COUNTER_FOR_UNIQUE_NAMES
        .fetch_add(1, Ordering::Relaxed)
        .hash(&mut hasher); // If there are multiple elements in the same call site
    hasher.finish()
}

fn ident_to_type(ident: &Ident) -> Cow<'static, Type> {
    Cow::Owned(
        TypePath {
            path: ident.clone().into(),
            qself: None,
        }
        .into(),
    )
}

fn escape_json_string(value: &str) -> String {
    let mut output = String::with_capacity(value.len());
    for c in value.chars() {
        match c {
            '\\' => output.push_str("\\\\"),
            '"' => output.push_str("\\\""),
            '\x08' => output.push_str("\\b"),
            '\x0C' => output.push_str("\\f"),
            '\n' => output.push_str("\\n"),
            '\r' => output.push_str("\\r"),
            '\t' => output.push_str("\\t"),
            c @ '\0'..='\x1F' => {
                write!(output, "\\u{:0>4x}", u32::from(c)).unwrap();
            }
            c => output.push(c),
        }
    }
    output
}