@@ -36,9 +36,12 @@ use std::mem;
3636use std:: ops;
3737use syntax:: abi;
3838use syntax:: ast;
39- use syntax:: codemap:: { Span , respan} ;
39+ use syntax:: codemap:: { DUMMY_SP , Span , respan} ;
4040use syntax:: ptr:: P ;
4141
42+ // Name of type defined in constified enum module
43+ pub static CONSTIFIED_ENUM_MODULE_REPR_NAME : & ' static str = "Type" ;
44+
4245fn root_import_depth ( ctx : & BindgenContext , item : & Item ) -> usize {
4346 if !ctx. options ( ) . enable_cxx_namespaces {
4447 return 0 ;
@@ -244,7 +247,6 @@ impl ForeignModBuilder {
244247 }
245248
246249 fn build ( self , ctx : & BindgenContext ) -> P < ast:: Item > {
247- use syntax:: codemap:: DUMMY_SP ;
248250 P ( ast:: Item {
249251 ident : ctx. rust_ident ( "" ) ,
250252 id : ast:: DUMMY_NODE_ID ,
@@ -2001,6 +2003,10 @@ enum EnumBuilder<'a> {
20012003 aster : P < ast:: Item > ,
20022004 } ,
20032005 Consts { aster : P < ast:: Item > } ,
2006+ ModuleConsts {
2007+ module_name : & ' a str ,
2008+ module_items : Vec < P < ast:: Item > > ,
2009+ } ,
20042010}
20052011
20062012impl < ' a > EnumBuilder < ' a > {
@@ -2010,7 +2016,8 @@ impl<'a> EnumBuilder<'a> {
20102016 name : & ' a str ,
20112017 repr : P < ast:: Ty > ,
20122018 bitfield_like : bool ,
2013- constify : bool )
2019+ constify : bool ,
2020+ constify_module : bool )
20142021 -> Self {
20152022 if bitfield_like {
20162023 EnumBuilder :: Bitfield {
@@ -2022,8 +2029,20 @@ impl<'a> EnumBuilder<'a> {
20222029 . build ( ) ,
20232030 }
20242031 } else if constify {
2025- EnumBuilder :: Consts {
2026- aster : aster. type_ ( name) . build_ty ( repr) ,
2032+ if constify_module {
2033+ let type_definition = aster:: item:: ItemBuilder :: new ( )
2034+ . pub_ ( )
2035+ . type_ ( CONSTIFIED_ENUM_MODULE_REPR_NAME )
2036+ . build_ty ( repr) ;
2037+
2038+ EnumBuilder :: ModuleConsts {
2039+ module_name : name,
2040+ module_items : vec ! [ type_definition] ,
2041+ }
2042+ } else {
2043+ EnumBuilder :: Consts {
2044+ aster : aster. type_ ( name) . build_ty ( repr) ,
2045+ }
20272046 }
20282047 } else {
20292048 EnumBuilder :: Rust ( aster. enum_ ( name) )
@@ -2095,6 +2114,27 @@ impl<'a> EnumBuilder<'a> {
20952114 result. push ( constant) ;
20962115 self
20972116 }
2117+ EnumBuilder :: ModuleConsts { module_name, module_items, .. } => {
2118+ // Variant type
2119+ let inside_module_type =
2120+ aster:: AstBuilder :: new ( ) . ty ( ) . id ( CONSTIFIED_ENUM_MODULE_REPR_NAME ) ;
2121+
2122+ let constant = aster:: AstBuilder :: new ( )
2123+ . item ( )
2124+ . pub_ ( )
2125+ . const_ ( & * variant_name)
2126+ . expr ( )
2127+ . build ( expr)
2128+ . build ( inside_module_type. clone ( ) ) ;
2129+
2130+ let mut module_items = module_items. clone ( ) ;
2131+ module_items. push ( constant) ;
2132+
2133+ EnumBuilder :: ModuleConsts {
2134+ module_name,
2135+ module_items,
2136+ }
2137+ }
20982138 }
20992139 }
21002140
@@ -2160,6 +2200,22 @@ impl<'a> EnumBuilder<'a> {
21602200 aster
21612201 }
21622202 EnumBuilder :: Consts { aster, .. } => aster,
2203+ EnumBuilder :: ModuleConsts { module_items, module_name, .. } => {
2204+ // Create module item with type and variant definitions
2205+ let module_item = P ( ast:: Item {
2206+ ident : ast:: Ident :: from_str ( module_name) ,
2207+ attrs : vec ! [ ] ,
2208+ id : ast:: DUMMY_NODE_ID ,
2209+ node : ast:: ItemKind :: Mod ( ast:: Mod {
2210+ inner : DUMMY_SP ,
2211+ items : module_items,
2212+ } ) ,
2213+ vis : ast:: Visibility :: Public ,
2214+ span : DUMMY_SP ,
2215+ } ) ;
2216+
2217+ module_item
2218+ }
21632219 }
21642220 }
21652221}
@@ -2225,7 +2281,10 @@ impl CodeGenerator for Enum {
22252281 . any ( |v| ctx. options ( ) . bitfield_enums . matches ( & v. name ( ) ) ) )
22262282 } ;
22272283
2228- let is_constified_enum = {
2284+ let is_constified_enum_module = self . is_constified_enum_module ( ctx, item) ;
2285+
2286+ let is_constified_enum = {
2287+ is_constified_enum_module ||
22292288 ctx. options ( ) . constified_enums . matches ( & name) ||
22302289 ( enum_ty. name ( ) . is_none ( ) &&
22312290 self . variants ( )
@@ -2300,7 +2359,8 @@ impl CodeGenerator for Enum {
23002359 & name,
23012360 repr,
23022361 is_bitfield,
2303- is_constified_enum) ;
2362+ is_constified_enum,
2363+ is_constified_enum_module) ;
23042364
23052365 // A map where we keep a value -> variant relation.
23062366 let mut seen_values = HashMap :: < _ , String > :: new ( ) ;
0 commit comments