@@ -10,11 +10,136 @@ mod attributes;
1010mod stability;
1111mod version;
1212
13+ use std:: num:: NonZero ;
14+
1315pub use attributes:: * ;
16+ use rustc_abi:: Align ;
17+ use rustc_ast:: token:: CommentKind ;
18+ use rustc_ast:: { AttrStyle , IntTy , UintTy } ;
19+ use rustc_ast_pretty:: pp:: Printer ;
20+ use rustc_span:: hygiene:: Transparency ;
21+ use rustc_span:: { Span , Symbol } ;
1422pub use stability:: * ;
23+ use thin_vec:: ThinVec ;
1524pub use version:: * ;
1625
1726/// Requirements for a `StableHashingContext` to be used in this crate.
1827/// This is a hack to allow using the `HashStable_Generic` derive macro
1928/// instead of implementing everything in `rustc_middle`.
2029pub trait HashStableContext : rustc_ast:: HashStableContext + rustc_abi:: HashStableContext { }
30+
31+ pub trait PrintAttribute {
32+ fn print_something ( & self ) -> bool ;
33+ fn print_attribute ( & self , p : & mut Printer ) ;
34+ }
35+
36+ impl < T : PrintAttribute > PrintAttribute for & T {
37+ fn print_something ( & self ) -> bool {
38+ T :: print_something ( self )
39+ }
40+
41+ fn print_attribute ( & self , p : & mut Printer ) {
42+ T :: print_attribute ( self , p)
43+ }
44+ }
45+ impl < T : PrintAttribute > PrintAttribute for Option < T > {
46+ fn print_something ( & self ) -> bool {
47+ self . as_ref ( ) . is_some_and ( |x| x. print_something ( ) )
48+ }
49+ fn print_attribute ( & self , p : & mut Printer ) {
50+ if let Some ( i) = self {
51+ T :: print_attribute ( i, p)
52+ }
53+ }
54+ }
55+ impl < T : PrintAttribute > PrintAttribute for ThinVec < T > {
56+ fn print_something ( & self ) -> bool {
57+ self . is_empty ( ) || self [ 0 ] . print_something ( )
58+ }
59+ fn print_attribute ( & self , p : & mut Printer ) {
60+ let mut last_printed = false ;
61+ p. word ( "[" ) ;
62+ for i in self {
63+ if last_printed {
64+ p. word_space ( "," ) ;
65+ }
66+ i. print_attribute ( p) ;
67+ last_printed = i. print_something ( ) ;
68+ }
69+ p. word ( "]" ) ;
70+ }
71+ }
72+ macro_rules! print_skip {
73+ ( $( $t: ty) ,* $( , ) ?) => { $(
74+ impl PrintAttribute for $t {
75+ fn print_something( & self ) -> bool { false }
76+ fn print_attribute( & self , _: & mut Printer ) { }
77+ } ) *
78+ } ;
79+ }
80+
81+ macro_rules! print_disp {
82+ ( $( $t: ty) ,* $( , ) ?) => { $(
83+ impl PrintAttribute for $t {
84+ fn print_something( & self ) -> bool { true }
85+ fn print_attribute( & self , p: & mut Printer ) {
86+ p. word( format!( "{}" , self ) ) ;
87+ }
88+ }
89+ ) * } ;
90+ }
91+ macro_rules! print_debug {
92+ ( $( $t: ty) ,* $( , ) ?) => { $(
93+ impl PrintAttribute for $t {
94+ fn print_something( & self ) -> bool { true }
95+ fn print_attribute( & self , p: & mut Printer ) {
96+ p. word( format!( "{:?}" , self ) ) ;
97+ }
98+ }
99+ ) * } ;
100+ }
101+
102+ macro_rules! print_tup {
103+ ( num_print_something $( $ts: ident) * ) => { 0 $( + $ts. print_something( ) as usize ) * } ;
104+ ( ) => { } ;
105+ ( $t: ident $( $ts: ident) * ) => {
106+ #[ allow( non_snake_case, unused) ]
107+ impl <$t: PrintAttribute , $( $ts: PrintAttribute ) ,* > PrintAttribute for ( $t, $( $ts) ,* ) {
108+ fn print_something( & self ) -> bool {
109+ let ( $t, $( $ts) ,* ) = self ;
110+ print_tup!( num_print_something $t $( $ts) * ) != 0
111+ }
112+
113+ fn print_attribute( & self , p: & mut Printer ) {
114+ let ( $t, $( $ts) ,* ) = self ;
115+ let parens = print_tup!( num_print_something $t $( $ts) * ) > 1 ;
116+ if parens {
117+ p. word( "(" ) ;
118+ }
119+
120+ let mut printed_anything = $t. print_something( ) ;
121+
122+ $t. print_attribute( p) ;
123+
124+ $(
125+ if printed_anything && $ts. print_something( ) {
126+ p. word_space( "," ) ;
127+ printed_anything = true ;
128+ }
129+ $ts. print_attribute( p) ;
130+ ) *
131+
132+ if parens {
133+ p. word( ")" ) ;
134+ }
135+ }
136+ }
137+
138+ print_tup!( $( $ts) * ) ;
139+ } ;
140+ }
141+
142+ print_tup ! ( A B C D E F G H ) ;
143+ print_skip ! ( Span , ( ) ) ;
144+ print_disp ! ( Symbol , u16 , bool , NonZero <u32 >) ;
145+ print_debug ! ( UintTy , IntTy , Align , AttrStyle , CommentKind , Transparency ) ;
0 commit comments