11use proc_macro:: TokenStream ;
22use proc_macro2:: TokenStream as TokenStream2 ;
3- use quote:: { quote, ToTokens } ;
3+ use quote:: { format_ident , quote, ToTokens } ;
44use syn:: parse_macro_input;
55
66use crate :: { cargo, construct} ;
@@ -9,7 +9,7 @@ use self::input::Input;
99
1010mod input;
1111
12- pub ( crate ) fn expand ( input : TokenStream ) -> TokenStream {
12+ pub ( crate ) fn expand ( input : TokenStream , is_v2 : bool ) -> TokenStream {
1313 let bitflags_input = TokenStream2 :: from ( input. clone ( ) ) ;
1414 let input = parse_macro_input ! ( input as Input ) ;
1515
@@ -23,11 +23,13 @@ pub(crate) fn expand(input: TokenStream) -> TokenStream {
2323 construct:: crate_local_disambiguator( ) ,
2424 cargo:: crate_name( ) ,
2525 ) ;
26- let format_tag = construct:: interned_string ( & format_string, "bitflags" , false , None ) ;
26+ let bitflags_tag = if is_v2 { "bitflagsv2" } else { "bitflags" } ;
27+ let format_tag = construct:: interned_string ( & format_string, bitflags_tag, false , None ) ;
2728
2829 let ident = input. ident ( ) ;
2930 let ty = input. ty ( ) ;
30- let flag_statics = codegen_flag_statics ( & input) ;
31+ let flag_statics = codegen_flag_statics ( & input, is_v2) ;
32+ let bitflag_macro = format_ident ! ( "{bitflags_tag}" ) ;
3133 quote ! (
3234 const _: ( ) = {
3335 fn assert<T : defmt:: export:: UnsignedInt >( ) { }
@@ -36,7 +38,7 @@ pub(crate) fn expand(input: TokenStream) -> TokenStream {
3638 #( #flag_statics) *
3739 } ;
3840
39- defmt:: export:: bitflags ! {
41+ defmt:: export:: #bitflag_macro ! {
4042 #bitflags_input
4143 }
4244
@@ -59,7 +61,7 @@ pub(crate) fn expand(input: TokenStream) -> TokenStream {
5961 . into ( )
6062}
6163
62- fn codegen_flag_statics ( input : & Input ) -> Vec < TokenStream2 > {
64+ fn codegen_flag_statics ( input : & Input , is_v2 : bool ) -> Vec < TokenStream2 > {
6365 input
6466 . flags ( )
6567 . enumerate ( )
@@ -69,11 +71,21 @@ fn codegen_flag_statics(input: &Input) -> Vec<TokenStream2> {
6971 let struct_name = input. ident ( ) ;
7072 let repr_ty = input. ty ( ) ;
7173
74+ let _tag = if is_v2 {
75+ "bitflagsv2_value"
76+ } else {
77+ "bitflags_value"
78+ } ;
7279 let sym_name = construct:: mangled_symbol_name (
73- "bitflags_value" ,
80+ _tag ,
7481 & format ! ( "{}::{i}::{}" , input. ident( ) , flag. ident( ) ) ,
7582 ) ;
7683
84+ let bits_access = if is_v2 {
85+ quote ! { bits( ) }
86+ } else {
87+ quote ! { bits}
88+ } ;
7789 quote ! {
7890 #( #cfg_attrs) *
7991 #[ cfg_attr( target_os = "macos" , link_section = ".defmt,end" ) ]
@@ -84,7 +96,7 @@ fn codegen_flag_statics(input: &Input) -> Vec<TokenStream2> {
8496 // causes a value such as `1 << 127` to be evaluated as an `i32`, which
8597 // overflows. So we instead coerce (but don't cast) it to the bitflags' raw
8698 // type, and then cast that to u128.
87- let coerced_value: #repr_ty = #struct_name:: #var_name. bits ;
99+ let coerced_value: #repr_ty = #struct_name:: #var_name. #bits_access ;
88100 coerced_value as u128
89101 } ;
90102 }
0 commit comments