forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_attribute.rs
150 lines (136 loc) · 4.64 KB
/
print_attribute.rs
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
use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned};
use syn::spanned::Spanned;
use syn::{Data, Fields, Ident};
use synstructure::Structure;
fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, TokenStream) {
let string_name = name.to_string();
let mut disps = vec![quote! {let mut __printed_anything = false;}];
match fields {
Fields::Named(fields_named) => {
let mut field_names = Vec::new();
for field in &fields_named.named {
let name = field.ident.as_ref().unwrap();
let string_name = name.to_string();
disps.push(quote! {
if #name.should_render() {
if __printed_anything {
__p.word_space(",");
}
__p.word(#string_name);
__p.word_space(":");
__printed_anything = true;
}
#name.print_attribute(__p);
});
field_names.push(name);
}
(
quote! { {#(#field_names),*} },
quote! {
__p.word(#string_name);
if true #(&& !#field_names.should_render())* {
return;
}
__p.nbsp();
__p.word("{");
#(#disps)*
__p.word("}");
},
quote! { true },
)
}
Fields::Unnamed(fields_unnamed) => {
let mut field_names = Vec::new();
for idx in 0..fields_unnamed.unnamed.len() {
let name = format_ident!("f{idx}");
disps.push(quote! {
if #name.should_render() {
if __printed_anything {
__p.word_space(",");
}
__printed_anything = true;
}
#name.print_attribute(__p);
});
field_names.push(name);
}
(
quote! { (#(#field_names),*) },
quote! {
__p.word(#string_name);
if true #(&& !#field_names.should_render())* {
return;
}
__p.popen();
#(#disps)*
__p.pclose();
},
quote! { true },
)
}
Fields::Unit => (quote! {}, quote! { __p.word(#string_name) }, quote! { true }),
}
}
pub(crate) fn print_attribute(input: Structure<'_>) -> TokenStream {
let span_error = |span, message: &str| {
quote_spanned! { span => const _: () = ::core::compile_error!(#message); }
};
// Must be applied to an enum type.
let (code, printed) = match &input.ast().data {
Data::Enum(e) => {
let (arms, printed) = e
.variants
.iter()
.map(|x| {
let ident = &x.ident;
let (pat, code, printed) = print_fields(ident, &x.fields);
(
quote! {
Self::#ident #pat => {#code}
},
quote! {
Self::#ident #pat => {#printed}
},
)
})
.unzip::<_, _, Vec<_>, Vec<_>>();
(
quote! {
match self {
#(#arms)*
}
},
quote! {
match self {
#(#printed)*
}
},
)
}
Data::Struct(s) => {
let (pat, code, printed) = print_fields(&input.ast().ident, &s.fields);
(
quote! {
let Self #pat = self;
#code
},
quote! {
let Self #pat = self;
#printed
},
)
}
Data::Union(u) => {
return span_error(u.union_token.span(), "can't derive PrintAttribute on unions");
}
};
#[allow(keyword_idents_2024)]
input.gen_impl(quote! {
#[allow(unused)]
gen impl PrintAttribute for @Self {
fn should_render(&self) -> bool { #printed }
fn print_attribute(&self, __p: &mut rustc_ast_pretty::pp::Printer) { #code }
}
})
}