| 1 | |
| 2 | |
| 3 | use proc_macro2::{Span, TokenStream};
|
| 4 | use quote::quote;
|
| 5 | use syn::{Data, DataEnum, DataStruct, DataUnion, Error, Type};
|
| 6 |
|
| 7 | use crate::{
|
| 8 | repr::{EnumRepr, StructUnionRepr},
|
| 9 | util::{
|
| 10 | generate_tag_enum, Ctx, DataExt, FieldBounds, ImplBlockBuilder, PaddingCheck, Trait,
|
| 11 | TraitBound,
|
| 12 | },
|
| 13 | };
|
| 14 | pub(crate) fn derive_into_bytes(ctx: &Ctx, _top_level: Trait) -> Result<TokenStream, Error> {
|
| 15 | match &ctx.ast.data {
|
| 16 | Data::Struct(strct) => derive_into_bytes_struct(ctx, strct),
|
| 17 | Data::Enum(enm) => derive_into_bytes_enum(ctx, enm),
|
| 18 | Data::Union(unn) => derive_into_bytes_union(ctx, unn),
|
| 19 | }
|
| 20 | }
|
| 21 | fn derive_into_bytes_struct(ctx: &Ctx, strct: &DataStruct) -> Result<TokenStream, Error> {
|
| 22 | let repr = StructUnionRepr::from_attrs(&ctx.ast.attrs)?;
|
| 23 |
|
| 24 | let is_transparent = repr.is_transparent();
|
| 25 | let is_c = repr.is_c();
|
| 26 | let is_packed_1 = repr.is_packed_1();
|
| 27 | let num_fields = strct.fields().len();
|
| 28 |
|
| 29 | let (padding_check, require_unaligned_fields) = if is_transparent || is_packed_1 {
|
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | (None, false)
|
| 47 | } else if is_c && !repr.is_align_gt_1() && num_fields <= 1 {
|
| 48 | |
| 49 | |
| 50 | |
| 51 | (None, false)
|
| 52 | } else if ctx.ast.generics.params.is_empty() {
|
| 53 | |
| 54 | let is_syntactic_dst =
|
| 55 | strct.fields().last().map(|(_, _, ty)| matches!(ty, Type::Slice(_))).unwrap_or(false);
|
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | if is_c && is_syntactic_dst {
|
| 69 | (Some(PaddingCheck::ReprCStruct), false)
|
| 70 | } else {
|
| 71 | (Some(PaddingCheck::Struct), false)
|
| 72 | }
|
| 73 | } else if is_c && !repr.is_align_gt_1() {
|
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | (None, true)
|
| 83 | } else {
|
| 84 | return ctx.error_or_skip(Error::new(
|
| 85 | Span::call_site(),
|
| 86 | "must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout",
|
| 87 | ));
|
| 88 | };
|
| 89 |
|
| 90 | let field_bounds = if require_unaligned_fields {
|
| 91 | FieldBounds::All(&[TraitBound::Slf, TraitBound::Other(Trait::Unaligned)])
|
| 92 | } else {
|
| 93 | FieldBounds::ALL_SELF
|
| 94 | };
|
| 95 |
|
| 96 | Ok(ImplBlockBuilder::new(ctx, strct, Trait::IntoBytes, field_bounds)
|
| 97 | .padding_check(padding_check)
|
| 98 | .build())
|
| 99 | }
|
| 100 |
|
| 101 | fn derive_into_bytes_enum(ctx: &Ctx, enm: &DataEnum) -> Result<TokenStream, Error> {
|
| 102 | let repr = EnumRepr::from_attrs(&ctx.ast.attrs)?;
|
| 103 | if !repr.is_c() && !repr.is_primitive() {
|
| 104 | return ctx.error_or_skip(Error::new(
|
| 105 | Span::call_site(),
|
| 106 | "must have #[repr(C)] or #[repr(Int)] attribute in order to guarantee this type's memory layout",
|
| 107 | ));
|
| 108 | }
|
| 109 |
|
| 110 | let tag_type_definition = generate_tag_enum(ctx, &repr, enm);
|
| 111 | Ok(ImplBlockBuilder::new(ctx, enm, Trait::IntoBytes, FieldBounds::ALL_SELF)
|
| 112 | .padding_check(PaddingCheck::Enum { tag_type_definition })
|
| 113 | .build())
|
| 114 | }
|
| 115 |
|
| 116 | fn derive_into_bytes_union(ctx: &Ctx, unn: &DataUnion) -> Result<TokenStream, Error> {
|
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | let cfg_compile_error = if cfg!(zerocopy_derive_union_into_bytes) {
|
| 126 | quote!()
|
| 127 | } else {
|
| 128 | let core = ctx.core_path();
|
| 129 | let error_message = "requires --cfg zerocopy_derive_union_into_bytes;
|
| 130 | please let us know you use this feature: https://github.com/google/zerocopy/discussions/1802";
|
| 131 | quote!(
|
| 132 | #[allow(unused_attributes, unexpected_cfgs)]
|
| 133 | const _: () = {
|
| 134 | #[cfg(not(zerocopy_derive_union_into_bytes))]
|
| 135 | #core::compile_error!(#error_message);
|
| 136 | };
|
| 137 | )
|
| 138 | };
|
| 139 |
|
| 140 | |
| 141 | if !ctx.ast.generics.params.is_empty() {
|
| 142 | return ctx.error_or_skip(Error::new(
|
| 143 | Span::call_site(),
|
| 144 | "unsupported on types with type parameters",
|
| 145 | ));
|
| 146 | }
|
| 147 |
|
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | let repr = StructUnionRepr::from_attrs(&ctx.ast.attrs)?;
|
| 153 | if !repr.is_c() && !repr.is_transparent() && !repr.is_packed_1() {
|
| 154 | return ctx.error_or_skip(Error::new(
|
| 155 | Span::call_site(),
|
| 156 | "must be #[repr(C)], #[repr(packed)], or #[repr(transparent)]",
|
| 157 | ));
|
| 158 | }
|
| 159 |
|
| 160 | let impl_block = ImplBlockBuilder::new(ctx, unn, Trait::IntoBytes, FieldBounds::ALL_SELF)
|
| 161 | .padding_check(PaddingCheck::Union)
|
| 162 | .build();
|
| 163 | Ok(quote!(#cfg_compile_error #impl_block))
|
| 164 | }
|