@@ -2129,36 +2129,54 @@ impl EnumVariation {
2129
2129
/// A helper type to construct different enum variations.
2130
2130
enum EnumBuilder < ' a > {
2131
2131
Rust {
2132
+ codegen_depth : usize ,
2132
2133
attrs : Vec < quote:: Tokens > ,
2133
2134
ident : proc_macro2:: Term ,
2134
2135
tokens : quote:: Tokens ,
2135
2136
emitted_any_variants : bool ,
2136
2137
} ,
2137
2138
Bitfield {
2139
+ codegen_depth : usize ,
2138
2140
canonical_name : & ' a str ,
2139
2141
tokens : quote:: Tokens ,
2140
2142
} ,
2141
- Consts ( Vec < quote:: Tokens > ) ,
2143
+ Consts {
2144
+ variants : Vec < quote:: Tokens > ,
2145
+ codegen_depth : usize ,
2146
+ } ,
2142
2147
ModuleConsts {
2148
+ codegen_depth : usize ,
2143
2149
module_name : & ' a str ,
2144
2150
module_items : Vec < quote:: Tokens > ,
2145
2151
} ,
2146
2152
}
2147
2153
2148
2154
impl < ' a > EnumBuilder < ' a > {
2155
+ /// Returns the depth of the code generation for a variant of this enum.
2156
+ fn codegen_depth ( & self ) -> usize {
2157
+ match * self {
2158
+ EnumBuilder :: Rust { codegen_depth, .. } |
2159
+ EnumBuilder :: Bitfield { codegen_depth, .. } |
2160
+ EnumBuilder :: ModuleConsts { codegen_depth, .. } |
2161
+ EnumBuilder :: Consts { codegen_depth, .. } => codegen_depth,
2162
+ }
2163
+ }
2164
+
2149
2165
/// Create a new enum given an item builder, a canonical name, a name for
2150
2166
/// the representation, and which variation it should be generated as.
2151
2167
fn new (
2152
2168
name : & ' a str ,
2153
2169
attrs : Vec < quote:: Tokens > ,
2154
2170
repr : quote:: Tokens ,
2155
- enum_variation : EnumVariation
2171
+ enum_variation : EnumVariation ,
2172
+ enum_codegen_depth : usize ,
2156
2173
) -> Self {
2157
2174
let ident = proc_macro2:: Term :: intern ( name) ;
2158
2175
2159
2176
match enum_variation {
2160
2177
EnumVariation :: Bitfield => {
2161
2178
EnumBuilder :: Bitfield {
2179
+ codegen_depth : enum_codegen_depth,
2162
2180
canonical_name : name,
2163
2181
tokens : quote ! {
2164
2182
#( #attrs ) *
@@ -2170,6 +2188,7 @@ impl<'a> EnumBuilder<'a> {
2170
2188
EnumVariation :: Rust => {
2171
2189
let tokens = quote ! ( ) ;
2172
2190
EnumBuilder :: Rust {
2191
+ codegen_depth : enum_codegen_depth + 1 ,
2173
2192
attrs,
2174
2193
ident,
2175
2194
tokens,
@@ -2178,20 +2197,26 @@ impl<'a> EnumBuilder<'a> {
2178
2197
}
2179
2198
2180
2199
EnumVariation :: Consts => {
2181
- EnumBuilder :: Consts ( vec ! [
2182
- quote! {
2183
- pub type #ident = #repr;
2184
- }
2185
- ] )
2200
+ EnumBuilder :: Consts {
2201
+ variants : vec ! [
2202
+ quote! {
2203
+ #( #attrs ) *
2204
+ pub type #ident = #repr;
2205
+ }
2206
+ ] ,
2207
+ codegen_depth : enum_codegen_depth,
2208
+ }
2186
2209
}
2187
2210
2188
2211
EnumVariation :: ModuleConsts => {
2189
2212
let ident = proc_macro2:: Term :: intern ( CONSTIFIED_ENUM_MODULE_REPR_NAME ) ;
2190
2213
let type_definition = quote ! {
2214
+ #( #attrs ) *
2191
2215
pub type #ident = #repr;
2192
2216
} ;
2193
2217
2194
2218
EnumBuilder :: ModuleConsts {
2219
+ codegen_depth : enum_codegen_depth + 1 ,
2195
2220
module_name : name,
2196
2221
module_items : vec ! [ type_definition] ,
2197
2222
}
@@ -2214,14 +2239,24 @@ impl<'a> EnumBuilder<'a> {
2214
2239
EnumVariantValue :: Unsigned ( v) => helpers:: ast_ty:: uint_expr ( v) ,
2215
2240
} ;
2216
2241
2242
+ let mut doc = quote ! { } ;
2243
+ if ctx. options ( ) . generate_comments {
2244
+ if let Some ( raw_comment) = variant. comment ( ) {
2245
+ let comment = comment:: preprocess ( raw_comment, self . codegen_depth ( ) ) ;
2246
+ doc = attributes:: doc ( comment) ;
2247
+ }
2248
+ }
2249
+
2217
2250
match self {
2218
- EnumBuilder :: Rust { attrs, ident, tokens, emitted_any_variants : _ } => {
2251
+ EnumBuilder :: Rust { attrs, ident, tokens, emitted_any_variants : _, codegen_depth } => {
2219
2252
let name = ctx. rust_ident ( variant_name) ;
2220
2253
EnumBuilder :: Rust {
2221
2254
attrs,
2222
2255
ident,
2256
+ codegen_depth,
2223
2257
tokens : quote ! {
2224
2258
#tokens
2259
+ #doc
2225
2260
#name = #expr,
2226
2261
} ,
2227
2262
emitted_any_variants : true ,
@@ -2238,6 +2273,7 @@ impl<'a> EnumBuilder<'a> {
2238
2273
2239
2274
let ident = ctx. rust_ident ( constant_name) ;
2240
2275
result. push ( quote ! {
2276
+ #doc
2241
2277
pub const #ident : #rust_ty = #rust_ty ( #expr ) ;
2242
2278
} ) ;
2243
2279
@@ -2256,24 +2292,28 @@ impl<'a> EnumBuilder<'a> {
2256
2292
2257
2293
let ident = ctx. rust_ident ( constant_name) ;
2258
2294
result. push ( quote ! {
2295
+ #doc
2259
2296
pub const #ident : #rust_ty = #expr ;
2260
2297
} ) ;
2261
2298
2262
2299
self
2263
2300
}
2264
2301
EnumBuilder :: ModuleConsts {
2302
+ codegen_depth,
2265
2303
module_name,
2266
2304
mut module_items,
2267
2305
} => {
2268
2306
let name = ctx. rust_ident ( variant_name) ;
2269
2307
let ty = ctx. rust_ident ( CONSTIFIED_ENUM_MODULE_REPR_NAME ) ;
2270
2308
module_items. push ( quote ! {
2309
+ #doc
2271
2310
pub const #name : #ty = #expr ;
2272
2311
} ) ;
2273
2312
2274
2313
EnumBuilder :: ModuleConsts {
2275
2314
module_name,
2276
2315
module_items,
2316
+ codegen_depth,
2277
2317
}
2278
2318
}
2279
2319
}
@@ -2286,23 +2326,24 @@ impl<'a> EnumBuilder<'a> {
2286
2326
result : & mut CodegenResult < ' b > ,
2287
2327
) -> quote:: Tokens {
2288
2328
match self {
2289
- EnumBuilder :: Rust { attrs, ident, tokens, emitted_any_variants } => {
2329
+ EnumBuilder :: Rust { attrs, ident, tokens, emitted_any_variants, .. } => {
2290
2330
let variants = if !emitted_any_variants {
2291
2331
quote ! ( __bindgen_cannot_repr_c_on_empty_enum = 0 )
2292
2332
} else {
2293
2333
tokens
2294
2334
} ;
2295
2335
2296
- quote ! (
2336
+ quote ! {
2297
2337
#( #attrs ) *
2298
2338
pub enum #ident {
2299
2339
#variants
2300
2340
}
2301
- )
2341
+ }
2302
2342
}
2303
2343
EnumBuilder :: Bitfield {
2304
2344
canonical_name,
2305
2345
tokens,
2346
+ ..
2306
2347
} => {
2307
2348
let rust_ty_name = ctx. rust_ident_raw ( canonical_name) ;
2308
2349
let prefix = ctx. trait_prefix ( ) ;
@@ -2349,10 +2390,11 @@ impl<'a> EnumBuilder<'a> {
2349
2390
2350
2391
tokens
2351
2392
}
2352
- EnumBuilder :: Consts ( tokens ) => quote ! { #( #tokens ) * } ,
2393
+ EnumBuilder :: Consts { variants , .. } => quote ! { #( #variants ) * } ,
2353
2394
EnumBuilder :: ModuleConsts {
2354
2395
module_items,
2355
2396
module_name,
2397
+ ..
2356
2398
} => {
2357
2399
let ident = ctx. rust_ident ( module_name) ;
2358
2400
quote ! {
@@ -2489,7 +2531,8 @@ impl CodeGenerator for Enum {
2489
2531
& name,
2490
2532
attrs,
2491
2533
repr,
2492
- variation
2534
+ variation,
2535
+ item. codegen_depth ( ctx) ,
2493
2536
) ;
2494
2537
2495
2538
// A map where we keep a value -> variant relation.
@@ -2522,8 +2565,7 @@ impl CodeGenerator for Enum {
2522
2565
let mut iter = self . variants ( ) . iter ( ) . peekable ( ) ;
2523
2566
while let Some ( variant) = iter. next ( ) . or_else ( || {
2524
2567
constified_variants. pop_front ( )
2525
- } )
2526
- {
2568
+ } ) {
2527
2569
if variant. hidden ( ) {
2528
2570
continue ;
2529
2571
}
0 commit comments