@@ -20,7 +20,7 @@ use ir::item_kind::ItemKind;
20
20
use ir:: layout:: Layout ;
21
21
use ir:: module:: Module ;
22
22
use ir:: objc:: ObjCInterface ;
23
- use ir:: template:: AsNamed ;
23
+ use ir:: template:: { AsNamed , TemplateInstantiation } ;
24
24
use ir:: ty:: { TemplateDeclaration , Type , TypeKind } ;
25
25
use ir:: var:: Var ;
26
26
@@ -518,14 +518,16 @@ impl CodeGenerator for Type {
518
518
TypeKind :: Pointer ( ..) |
519
519
TypeKind :: BlockPointer |
520
520
TypeKind :: Reference ( ..) |
521
- TypeKind :: TemplateInstantiation ( ..) |
522
521
TypeKind :: Function ( ..) |
523
522
TypeKind :: ResolvedTypeRef ( ..) |
524
523
TypeKind :: Named => {
525
524
// These items don't need code generation, they only need to be
526
525
// converted to rust types in fields, arguments, and such.
527
526
return ;
528
527
}
528
+ TypeKind :: TemplateInstantiation ( ref inst) => {
529
+ inst. codegen ( ctx, result, whitelisted_items, item)
530
+ }
529
531
TypeKind :: Comp ( ref ci) => {
530
532
ci. codegen ( ctx, result, whitelisted_items, item)
531
533
}
@@ -821,6 +823,53 @@ impl<'a> Bitfield<'a> {
821
823
}
822
824
}
823
825
826
+ impl CodeGenerator for TemplateInstantiation {
827
+ type Extra = Item ;
828
+
829
+ fn codegen < ' a > ( & self ,
830
+ ctx : & BindgenContext ,
831
+ result : & mut CodegenResult < ' a > ,
832
+ _whitelisted_items : & ItemSet ,
833
+ item : & Item ) {
834
+ // Although uses of instantiations don't need code generation, and are
835
+ // just converted to rust types in fields, vars, etc, we take this
836
+ // opportunity to generate tests for their layout here.
837
+
838
+ let layout = item. kind ( ) . expect_type ( ) . layout ( ctx) ;
839
+
840
+ if let Some ( layout) = layout {
841
+ let size = layout. size ;
842
+ let align = layout. align ;
843
+
844
+ let name = item. canonical_name ( ctx) ;
845
+ let fn_name = format ! ( "__bindgen_test_layout_{}_instantiation_{}" ,
846
+ name,
847
+ item. id( ) . as_usize( ) ) ;
848
+ let fn_name = ctx. rust_ident_raw ( & fn_name) ;
849
+
850
+ let prefix = ctx. trait_prefix ( ) ;
851
+ let ident = item. to_rust_ty ( ctx) ;
852
+ let size_of_expr = quote_expr ! ( ctx. ext_cx( ) ,
853
+ :: $prefix:: mem:: size_of:: <$ident>( ) ) ;
854
+ let align_of_expr = quote_expr ! ( ctx. ext_cx( ) ,
855
+ :: $prefix:: mem:: align_of:: <$ident>( ) ) ;
856
+
857
+ let item = quote_item ! (
858
+ ctx. ext_cx( ) ,
859
+ #[ test]
860
+ fn $fn_name( ) {
861
+ assert_eq!( $size_of_expr, $size,
862
+ concat!( "Size of template specialization: " , stringify!( $ident) ) ) ;
863
+ assert_eq!( $align_of_expr, $align,
864
+ concat!( "Alignment of template specialization: " , stringify!( $ident) ) ) ;
865
+ } )
866
+ . unwrap ( ) ;
867
+
868
+ result. push ( item) ;
869
+ }
870
+ }
871
+ }
872
+
824
873
impl CodeGenerator for CompInfo {
825
874
type Extra = Item ;
826
875
0 commit comments