reallyme-buffa-codegen 0.1.0

ReallyMe fork: Shared code generation logic for buffa (descriptor → Rust source)
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
// Modified by ReallyMe Technologies Limited.
//! Code generation for vtable-mode reflection on owned message types.
//!
//! Parallel to [`reflect_view`](crate::reflect_view), but for the owned struct
//! rather than the zero-copy view. When vtable mode is on, each owned message
//! gets:
//!
//! - `impl ReflectMessage for Foo` — reads owned struct fields directly
//!   (`String`/`Vec<u8>`/`MessageField`/`Vec`/`HashMap`/owned oneof enum),
//!   backed by the owned-container reflect impls in `buffa-descriptor`.
//! - `impl ReflectElement for Foo` — so a `Vec<Foo>` / `HashMap<_, Foo>`
//!   reflects through the generic container impls.
//! - A memoized per-message `MessageIndex` accessor.
//!
//! The owned `Reflectable::reflect()` body is then `ReflectCow::Borrowed(self)`
//! (emitted by [`reflect`](crate::reflect)), so reflecting an in-memory message
//! costs no encode/decode round-trip — the interceptor use case. Bridge mode
//! keeps the round-trip body and emits none of this.

use std::collections::HashMap;

use proc_macro2::TokenStream;
use quote::quote;

use crate::context::CodeGenContext;
use crate::features::{resolve_field, ResolvedFeatures};
use crate::generated::descriptor::field_descriptor_proto::{Label, Type};
use crate::generated::descriptor::DescriptorProto;
use crate::impl_message::{
    effective_type, is_explicit_presence_scalar, is_real_oneof_member, is_supported_field_type,
};
use crate::message::{is_closed_enum, is_map_field, rust_path_to_tokens};
use crate::oneof::oneof_variant_ident;
use crate::reflect_view::{enum_default_number, scalar_default, scalar_variant};
use crate::CodeGenError;

/// Context needed to emit the owned-message vtable impls, mirroring the
/// arguments [`generate_message_impl`](crate::impl_message::generate_message_impl)
/// already has in hand.
pub(crate) struct OwnedReflectScope<'a> {
    pub ctx: &'a CodeGenContext<'a>,
    pub msg: &'a DescriptorProto,
    pub name_ident: &'a proc_macro2::Ident,
    pub buffa_path: &'a TokenStream,
    pub current_package: &'a str,
    /// Message path without a leading dot (e.g. `pkg.Outer.Inner`), used to
    /// build variant paths for `variant_boxed` lookups.
    pub proto_fqn: &'a str,
    pub features: &'a ResolvedFeatures,
    pub oneof_idents: &'a HashMap<usize, proc_macro2::Ident>,
    pub oneof_prefix: &'a TokenStream,
    pub nesting: usize,
}

/// Generate `impl ReflectMessage` + `impl ReflectElement` + the memoized
/// `MessageIndex` accessor for an owned message.
pub(crate) fn reflect_owned_impls(
    scope: &OwnedReflectScope<'_>,
) -> Result<TokenStream, CodeGenError> {
    let ctx = scope.ctx;
    let msg = scope.msg;
    let name_ident = scope.name_ident;
    let buffa_path = scope.buffa_path;
    let current_package = scope.current_package;
    let proto_fqn = scope.proto_fqn;
    let features = scope.features;
    let deprecated_field_allow = crate::message::deprecated_field_allow(ctx, msg, proto_fqn);
    let oneof_idents = scope.oneof_idents;
    let oneof_prefix = scope.oneof_prefix;
    let nesting = scope.nesting;
    let guarded = ctx.zeroizes_message(proto_fqn);
    let vr = if guarded {
        quote! { ::buffa_descriptor::reflect::InspectValueRef }
    } else {
        quote! { ::buffa_descriptor::reflect::ValueRef }
    };
    let cow = if guarded {
        quote! { ::buffa_descriptor::reflect::InspectMessageRef }
    } else {
        quote! { ::buffa_descriptor::reflect::ReflectCow }
    };
    // Message-typed fields route through the field type's own
    // `Reflectable::reflect()` rather than `ReflectCow::Borrowed` directly:
    // a vtable-grade field returns `Borrowed` (zero-cost, same as before),
    // while a bridge-grade field from another compilation degrades to an
    // owned `DynamicMessage` snapshot at the boundary — the mixed-mode
    // behavior the reflection design promises.
    let reflect = if guarded {
        quote! { ::buffa_descriptor::reflect::InspectMessageRef::new }
    } else {
        quote! { ::buffa_descriptor::reflect::Reflectable::reflect }
    };

    let mut get_arms: Vec<TokenStream> = Vec::new();
    let mut has_arms: Vec<TokenStream> = Vec::new();

    // Direct (non-oneof) fields.
    for field in &msg.field {
        if is_real_oneof_member(field) {
            continue;
        }
        let ty = effective_type(ctx, field, features);
        if !is_supported_field_type(ty) {
            continue;
        }
        let name = field
            .name
            .as_deref()
            .ok_or(CodeGenError::MissingField("field.name"))?;
        let id = ctx.field_ident(name, field.number.unwrap_or(0));
        let number = field.number.unwrap_or(0) as u32;
        let is_repeated = field.label.unwrap_or_default() == Label::LABEL_REPEATED;

        if is_repeated && is_map_field(msg, field) {
            get_arms.push(quote! { #number => #vr::Map(&self.#id), });
            // The default `HashMap` keeps `.is_empty()` (byte-identical output);
            // a `BTreeMap` or custom map is checked through the generic
            // `MapStorage` surface, which any configured map implements.
            if crate::impl_message::field_map_repr(ctx, proto_fqn, name).is_default() {
                has_arms.push(quote! { #number => !self.#id.is_empty(), });
            } else {
                has_arms.push(quote! {
                    #number => ::buffa::map_codec::MapStorage::storage_len(&self.#id) != 0,
                });
            }
            continue;
        }
        if is_repeated {
            get_arms.push(quote! { #number => #vr::List(&self.#id), });
            has_arms.push(quote! { #number => !self.#id.is_empty(), });
            continue;
        }

        let f_features = resolve_field(ctx, field, features);
        let (get_val, has_val) = if is_explicit_presence_scalar(field, ty, &f_features) {
            // Stored as `Option<T>`; absent singular returns the type default.
            match ty {
                Type::TYPE_STRING => (
                    quote! { #vr::String(self.#id.as_deref().unwrap_or("")) },
                    quote! { self.#id.is_some() },
                ),
                Type::TYPE_BYTES => (
                    quote! { #vr::Bytes(self.#id.as_deref().unwrap_or(&[])) },
                    quote! { self.#id.is_some() },
                ),
                Type::TYPE_ENUM => {
                    let default = enum_default_number(ctx, field, current_package, nesting)?;
                    (
                        quote! {
                            #vr::EnumNumber(self.#id.map_or_else(|| #default, |e| e.to_i32()))
                        },
                        quote! { self.#id.is_some() },
                    )
                }
                _ => {
                    let variant = scalar_variant(ty);
                    let def = scalar_default(ty);
                    (
                        quote! { #vr::#variant(self.#id.unwrap_or(#def)) },
                        quote! { self.#id.is_some() },
                    )
                }
            }
        } else {
            // Implicit presence: absent is the default value, present is
            // non-default. proto2 `required` fields also fall here (stored as
            // bare types), so a required field set to its default reflects as
            // `has() == false` — the same limitation the view path documents.
            match ty {
                Type::TYPE_STRING => (
                    quote! { #vr::String(&self.#id) },
                    quote! { !self.#id.is_empty() },
                ),
                Type::TYPE_BYTES => (
                    quote! { #vr::Bytes(&self.#id[..]) },
                    quote! { !self.#id.is_empty() },
                ),
                Type::TYPE_MESSAGE | Type::TYPE_GROUP => (
                    // `MessageField` derefs to the inner message, or the static
                    // default instance when unset, so the borrow is always valid.
                    quote! { #vr::Message(#reflect(&*self.#id)) },
                    quote! { self.#id.is_set() },
                ),
                Type::TYPE_ENUM => {
                    // Closed enums compare against the type default (which
                    // need not be zero); enums opened by an enum-type override keep
                    // their declared default the same way.
                    let has_val = if is_closed_enum(&f_features) {
                        quote! { self.#id != ::core::default::Default::default() }
                    } else if let Some(default_expr) =
                        crate::defaults::open_enum_bare_default_value(
                            field,
                            ctx,
                            current_package,
                            &f_features,
                            nesting,
                        )?
                    {
                        quote! { self.#id != #default_expr }
                    } else {
                        quote! { self.#id.to_i32() != 0 }
                    };
                    (quote! { #vr::EnumNumber(self.#id.to_i32()) }, has_val)
                }
                _ => {
                    let variant = scalar_variant(ty);
                    let has_val = match ty {
                        Type::TYPE_BOOL => quote! { self.#id },
                        // By bit pattern, so `-0.0` and NaN are set, as in the
                        // encoder's `is_non_default_expr`.
                        Type::TYPE_FLOAT | Type::TYPE_DOUBLE => {
                            quote! { self.#id.to_bits() != 0 }
                        }
                        _ => quote! { self.#id != 0 },
                    };
                    (quote! { #vr::#variant(self.#id) }, has_val)
                }
            }
        };
        get_arms.push(quote! { #number => #get_val, });
        has_arms.push(quote! { #number => #has_val, });
    }

    // Oneof members dispatch through the `Option<Kind>` struct field.
    for (idx, oneof) in msg.oneof_decl.iter().enumerate() {
        let Some(base_ident) = oneof_idents.get(&idx) else {
            continue;
        };
        let oneof_name = oneof
            .name
            .as_deref()
            .ok_or(CodeGenError::MissingField("oneof.name"))?;
        let field_ident = ctx.oneof_ident(oneof_name);
        let oneof_enum = quote! { #oneof_prefix #base_ident };

        for field in msg
            .field
            .iter()
            .filter(|f| is_real_oneof_member(f) && f.oneof_index == Some(idx as i32))
        {
            let name = field
                .name
                .as_deref()
                .ok_or(CodeGenError::MissingField("field.name"))?;
            let number = field.number.unwrap_or(0) as u32;
            let variant = oneof_variant_ident(name);
            let ty = effective_type(ctx, field, features);

            let (active, default) = match ty {
                Type::TYPE_STRING => (quote! { #vr::String(v) }, quote! { #vr::String("") }),
                Type::TYPE_BYTES => (quote! { #vr::Bytes(&v[..]) }, quote! { #vr::Bytes(&[]) }),
                Type::TYPE_MESSAGE | Type::TYPE_GROUP => {
                    let owned_ty = resolve_owned_message_ty(ctx, field, current_package, nesting)?;
                    // Boxed variants bind `v: &Box<M>` (deref twice to reach
                    // `&M`); variants opted out via unbox_oneof store the
                    // message inline and bind `v: &M` directly.
                    let borrowed = if crate::oneof::variant_boxed(
                        ctx,
                        ty,
                        &format!(".{proto_fqn}.{oneof_name}.{name}"),
                    ) {
                        quote! { &**v }
                    } else {
                        quote! { v }
                    };
                    (
                        quote! { #vr::Message(#reflect(#borrowed)) },
                        quote! {
                            #vr::Message(#reflect(
                                <#owned_ty as ::buffa::DefaultInstance>::default_instance(),
                            ))
                        },
                    )
                }
                Type::TYPE_ENUM => {
                    let default = enum_default_number(ctx, field, current_package, nesting)?;
                    (
                        quote! { #vr::EnumNumber(v.to_i32()) },
                        quote! { #vr::EnumNumber(#default) },
                    )
                }
                _ => {
                    let variant_v = scalar_variant(ty);
                    let def = scalar_default(ty);
                    (
                        quote! { #vr::#variant_v(*v) },
                        quote! { #vr::#variant_v(#def) },
                    )
                }
            };

            get_arms.push(quote! {
                #number => match &self.#field_ident {
                    ::core::option::Option::Some(#oneof_enum::#variant(v)) => #active,
                    _ => #default,
                },
            });
            has_arms.push(quote! {
                #number => ::core::matches!(
                    &self.#field_ident,
                    ::core::option::Option::Some(#oneof_enum::#variant(_))
                ),
            });
        }
    }

    let pool = quote! { #buffa_path::reflect::descriptor_pool() };

    if guarded {
        return Ok(crate::inspect::message_impl(
            &quote! {},
            &quote! { #name_ident },
            &pool,
            &get_arms,
            &has_arms,
            &deprecated_field_allow,
        ));
    }

    // Preserve unknown fields through reflection, matching the bridge path
    // (`DynamicMessage` carries them). Without this override the trait default
    // returns an empty set, so a recursive reflective walk over nested messages
    // would silently drop fields the local schema doesn't know — the exact
    // regression `ReflectMessage::unknown_fields`'s own doc warns against.
    let unknown_fields_method = if ctx.preserve_unknown_fields(proto_fqn) {
        quote! {
            fn unknown_fields(&self) -> &::buffa::UnknownFields {
                &self.__buffa_unknown_fields
            }
        }
    } else {
        quote! {}
    };

    Ok(quote! {
        #deprecated_field_allow
        impl ::buffa_descriptor::reflect::ReflectMessage for #name_ident {
            fn message_descriptor(&self) -> &::buffa_descriptor::MessageDescriptor {
                #pool.message(Self::__buffa_reflect_message_index())
            }

            fn pool(&self) -> &::buffa::alloc::sync::Arc<::buffa_descriptor::DescriptorPool> {
                #pool
            }

            #unknown_fields_method

            fn get(&self, field: &::buffa_descriptor::FieldDescriptor) -> #vr<'_> {
                // Closed enums use the `Enumeration` trait `to_i32`; open enums
                // (`EnumValue`) use the inherent one. No-op import for messages
                // without enum fields.
                #[allow(unused_imports)]
                use ::buffa::Enumeration as _;
                match field.number() {
                    #(#get_arms)*
                    _ => {
                        ::core::debug_assert!(
                            false,
                            "field number {} is not a member of this message's reflect get()",
                            field.number(),
                        );
                        #vr::Bool(false)
                    }
                }
            }

            fn has(&self, field: &::buffa_descriptor::FieldDescriptor) -> bool {
                match field.number() {
                    #(#has_arms)*
                    _ => false,
                }
            }

            fn for_each_set(
                &self,
                f: &mut dyn ::core::ops::FnMut(&::buffa_descriptor::FieldDescriptor, #vr<'_>),
            ) {
                let md = ::buffa_descriptor::reflect::ReflectMessage::message_descriptor(self);
                for fd in md.fields() {
                    if ::buffa_descriptor::reflect::ReflectMessage::has(self, fd) {
                        f(fd, ::buffa_descriptor::reflect::ReflectMessage::get(self, fd));
                    }
                }
            }

            fn to_dynamic(&self) -> ::buffa_descriptor::reflect::DynamicMessage {
                ::buffa_descriptor::reflect::DynamicMessage::from_message(
                    self,
                    ::buffa::alloc::sync::Arc::clone(#pool),
                    Self::__buffa_reflect_message_index(),
                )
            }
        }

        // `#[inline]` for the same cross-crate zero-cost reason as the
        // vtable `Reflectable::reflect()` body (see reflect.rs).
        impl ::buffa_descriptor::reflect::ReflectElement for #name_ident {
            #[inline]
            fn as_value_ref(&self) -> #vr<'_> {
                #vr::Message(#cow::Borrowed(self))
            }
        }

        impl #name_ident {
            /// Memoized `MessageIndex` for this message type, resolved once
            /// against the package's embedded descriptor pool.
            #[doc(hidden)]
            fn __buffa_reflect_message_index() -> ::buffa_descriptor::MessageIndex {
                static IDX: ::std::sync::OnceLock<::buffa_descriptor::MessageIndex> =
                    ::std::sync::OnceLock::new();
                *IDX.get_or_init(|| {
                    #pool
                        .message_index(<Self as ::buffa::MessageName>::FULL_NAME)
                        .expect("generated message is registered in the embedded descriptor pool")
                })
            }
        }
    })
}

/// Resolve the owned Rust type token for a message-typed oneof member, used for
/// the unset-member default (`<Ty as DefaultInstance>::default_instance()`).
fn resolve_owned_message_ty(
    ctx: &CodeGenContext,
    field: &crate::generated::descriptor::FieldDescriptorProto,
    current_package: &str,
    nesting: usize,
) -> Result<TokenStream, CodeGenError> {
    let dotted = field
        .type_name
        .as_deref()
        .ok_or(CodeGenError::MissingField("field.type_name"))?;
    let path = ctx
        .rust_type_relative(dotted, current_package, nesting)
        .ok_or_else(|| {
            CodeGenError::Other(format!(
                "owned type for oneof message '{dotted}' not resolvable"
            ))
        })?;
    Ok(rust_path_to_tokens(&path))
}