@@ -144,7 +144,7 @@ impl<'a> GenericTypes<'a> {
144
144
}
145
145
146
146
assert_simple_bound ( & trait_bound) ;
147
- if let Some ( mut path) = types. maybe_resolve_path ( & trait_bound. path ) {
147
+ if let Some ( mut path) = types. maybe_resolve_path ( & trait_bound. path , None ) {
148
148
if types. skip_path ( & path) { continue ; }
149
149
if non_lifetimes_processed { return false ; }
150
150
non_lifetimes_processed = true ;
@@ -868,7 +868,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
868
868
} else { None }
869
869
}
870
870
871
- pub fn maybe_resolve_path ( & self , p : & syn:: Path ) -> Option < String > {
871
+ pub fn maybe_resolve_path ( & self , p_arg : & syn:: Path , generics : Option < & GenericTypes > ) -> Option < String > {
872
+ let p = if let Some ( gen_types) = generics {
873
+ if let Some ( ( _, synpath) ) = gen_types. maybe_resolve_path ( p_arg) {
874
+ synpath
875
+ } else { p_arg }
876
+ } else { p_arg } ;
877
+
872
878
if p. leading_colon . is_some ( ) {
873
879
// At some point we may need this, but for now, its unused, so just fail.
874
880
return None ;
@@ -898,15 +904,15 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
898
904
}
899
905
}
900
906
pub fn resolve_path ( & self , p : & syn:: Path ) -> String {
901
- self . maybe_resolve_path ( p) . unwrap ( )
907
+ self . maybe_resolve_path ( p, None ) . unwrap ( )
902
908
}
903
909
904
910
// ***********************************
905
911
// *** Original Rust Type Printing ***
906
912
// ***********************************
907
913
908
914
fn write_rust_path < W : std:: io:: Write > ( & self , w : & mut W , path : & syn:: Path ) {
909
- if let Some ( resolved) = self . maybe_resolve_path ( & path) {
915
+ if let Some ( resolved) = self . maybe_resolve_path ( & path, None ) {
910
916
if self . is_primitive ( & resolved) {
911
917
write ! ( w, "{}" , path. get_ident( ) . unwrap( ) ) . unwrap ( ) ;
912
918
} else {
@@ -1119,12 +1125,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1119
1125
match t {
1120
1126
syn:: Type :: Path ( p) => {
1121
1127
if p. qself . is_some ( ) { unimplemented ! ( ) ; }
1122
- if let Some ( gen_types) = generics {
1123
- if let Some ( resolved) = gen_types. maybe_resolve_path ( & p. path ) {
1124
- return self . skip_path ( resolved. 0 ) ;
1125
- }
1126
- }
1127
- if let Some ( full_path) = self . maybe_resolve_path ( & p. path ) {
1128
+ if let Some ( full_path) = self . maybe_resolve_path ( & p. path , generics) {
1128
1129
self . skip_path ( & full_path)
1129
1130
} else { false }
1130
1131
} ,
@@ -1138,13 +1139,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1138
1139
match t {
1139
1140
syn:: Type :: Path ( p) => {
1140
1141
if p. qself . is_some ( ) { unimplemented ! ( ) ; }
1141
- if let Some ( gen_types) = generics {
1142
- if let Some ( resolved) = gen_types. maybe_resolve_path ( & p. path ) {
1143
- write ! ( w, "{}" , self . no_arg_path_to_rust( resolved. 0 ) ) . unwrap ( ) ;
1144
- return ;
1145
- }
1146
- }
1147
- if let Some ( full_path) = self . maybe_resolve_path ( & p. path ) {
1142
+ if let Some ( full_path) = self . maybe_resolve_path ( & p. path , generics) {
1148
1143
write ! ( w, "{}" , self . no_arg_path_to_rust( & full_path) ) . unwrap ( ) ;
1149
1144
}
1150
1145
} ,
@@ -1185,7 +1180,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1185
1180
// crate_types lookup has to have succeeded:
1186
1181
panic ! ( "Failed to print inline conversion for {}" , synident) ;
1187
1182
} else if let Some ( decl_type) = self . declared . get ( synident) {
1188
- decl_lookup ( w, decl_type, & self . maybe_resolve_path ( synpath) . unwrap ( ) , is_ref, is_mut) ;
1183
+ decl_lookup ( w, decl_type, & self . maybe_resolve_path ( synpath, None ) . unwrap ( ) , is_ref, is_mut) ;
1189
1184
return ;
1190
1185
} else { unimplemented ! ( ) ; }
1191
1186
}
@@ -1616,7 +1611,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1616
1611
// *** C Container Type Equivalent and alias Printing ***
1617
1612
// ******************************************************
1618
1613
1619
- fn write_template_constructor < W : std:: io:: Write > ( & mut self , w : & mut W , container_type : & str , mangled_container : & str , args : & Vec < & syn:: Type > , is_ref : bool ) {
1614
+ fn write_template_constructor < W : std:: io:: Write > ( & mut self , w : & mut W , container_type : & str , mangled_container : & str , args : & Vec < & syn:: Type > , generics : Option < & GenericTypes > , is_ref : bool ) {
1620
1615
if container_type == "Result" {
1621
1616
assert_eq ! ( args. len( ) , 2 ) ;
1622
1617
macro_rules! write_fn {
@@ -1631,13 +1626,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1631
1626
if let syn:: Type :: Path ( syn:: TypePath { path, .. } ) = $item {
1632
1627
let resolved = self . resolve_path( path) ;
1633
1628
if self . is_known_container( & resolved, is_ref) || self . is_transparent_container( & resolved, is_ref) {
1634
- self . write_c_mangled_container_path_intern( w, Self :: path_to_generic_args( path) ,
1629
+ self . write_c_mangled_container_path_intern( w, Self :: path_to_generic_args( path) , generics ,
1635
1630
& format!( "{}" , single_ident_generic_path_to_ident( path) . unwrap( ) ) , is_ref, false , false , false ) ;
1636
1631
} else {
1637
1632
self . write_template_generics( w, & mut [ $item] . iter( ) . map( |t| * t) , is_ref, true ) ;
1638
1633
}
1639
1634
} else if let syn:: Type :: Tuple ( syn:: TypeTuple { elems, .. } ) = $item {
1640
- self . write_c_mangled_container_path_intern( w, elems. iter( ) . collect( ) ,
1635
+ self . write_c_mangled_container_path_intern( w, elems. iter( ) . collect( ) , generics ,
1641
1636
& format!( "{}Tuple" , elems. len( ) ) , is_ref, false , false , false ) ;
1642
1637
} else { unimplemented!( ) ; }
1643
1638
write!( w, ") -> {} =\n \t {}::CResultTempl::<" , mangled_container, Self :: container_templ_path( ) ) . unwrap( ) ;
@@ -1657,7 +1652,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1657
1652
write ! ( w, "#[no_mangle]\n pub extern \" C\" fn {}_new(" , mangled_container) . unwrap ( ) ;
1658
1653
for ( idx, gen) in args. iter ( ) . enumerate ( ) {
1659
1654
write ! ( w, "{}{}: " , if idx != 0 { ", " } else { "" } , ( 'a' as u8 + idx as u8 ) as char ) . unwrap ( ) ;
1660
- self . write_c_type_intern ( None , w, gen, false , false , false ) ;
1655
+ self . write_c_type_intern ( w, gen, None , false , false , false ) ;
1661
1656
}
1662
1657
writeln ! ( w, ") -> {} {{" , mangled_container) . unwrap ( ) ;
1663
1658
writeln ! ( w, "\t {} {{" , mangled_container) . unwrap ( ) ;
@@ -1745,7 +1740,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1745
1740
}
1746
1741
}
1747
1742
}
1748
- fn check_create_container ( & mut self , mangled_container : String , container_type : & str , args : Vec < & syn:: Type > , is_ref : bool ) {
1743
+ fn check_create_container ( & mut self , mangled_container : String , container_type : & str , args : Vec < & syn:: Type > , generics : Option < & GenericTypes > , is_ref : bool ) {
1749
1744
if !self . crate_types . templates_defined . get ( & mangled_container) . is_some ( ) {
1750
1745
self . crate_types . templates_defined . insert ( mangled_container. clone ( ) , true ) ;
1751
1746
let mut created_container: Vec < u8 > = Vec :: new ( ) ;
@@ -1760,7 +1755,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1760
1755
self . write_template_generics ( & mut created_container, & mut args. iter ( ) . map ( |t| * t) , is_ref, true ) ;
1761
1756
writeln ! ( & mut created_container, ">;" ) . unwrap ( ) ;
1762
1757
1763
- self . write_template_constructor ( & mut created_container, container_type, & mangled_container, & args, is_ref) ;
1758
+ self . write_template_constructor ( & mut created_container, container_type, & mangled_container, & args, generics , is_ref) ;
1764
1759
1765
1760
self . crate_types . template_file . write ( & created_container) . unwrap ( ) ;
1766
1761
}
@@ -1771,7 +1766,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1771
1766
} else { unimplemented ! ( ) ; }
1772
1767
}
1773
1768
fn write_c_mangled_container_path_intern < W : std:: io:: Write >
1774
- ( & mut self , w : & mut W , args : Vec < & syn:: Type > , ident : & str , is_ref : bool , is_mut : bool , ptr_for_ref : bool , in_type : bool ) -> bool {
1769
+ ( & mut self , w : & mut W , args : Vec < & syn:: Type > , generics : Option < & GenericTypes > , ident : & str , is_ref : bool , is_mut : bool , ptr_for_ref : bool , in_type : bool ) -> bool {
1775
1770
let mut mangled_type: Vec < u8 > = Vec :: new ( ) ;
1776
1771
if !self . is_transparent_container ( ident, is_ref) {
1777
1772
write ! ( w, "C{}_" , ident) . unwrap ( ) ;
@@ -1788,9 +1783,9 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1788
1783
if self . is_known_container( & subtype, is_ref) { return false ; }
1789
1784
if !in_type {
1790
1785
if self . c_type_has_inner_from_path( & subtype) {
1791
- if !self . write_c_path_intern( w, & $p_arg. path, is_ref, is_mut, ptr_for_ref) { return false ; }
1786
+ if !self . write_c_path_intern( w, & $p_arg. path, generics , is_ref, is_mut, ptr_for_ref) { return false ; }
1792
1787
} else {
1793
- if !self . write_c_path_intern( w, & $p_arg. path, true , is_mut, true ) { return false ; }
1788
+ if !self . write_c_path_intern( w, & $p_arg. path, generics , true , is_mut, true ) { return false ; }
1794
1789
}
1795
1790
} else {
1796
1791
if $p_arg. path. segments. len( ) == 1 {
@@ -1800,15 +1795,15 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1800
1795
}
1801
1796
}
1802
1797
} else if self . is_known_container( & subtype, is_ref) || self . is_transparent_container( & subtype, is_ref) {
1803
- if !self . write_c_mangled_container_path_intern( w, Self :: path_to_generic_args( & $p_arg. path) ,
1798
+ if !self . write_c_mangled_container_path_intern( w, Self :: path_to_generic_args( & $p_arg. path) , generics ,
1804
1799
& subtype, is_ref, is_mut, ptr_for_ref, true ) {
1805
1800
return false ;
1806
1801
}
1807
1802
self . write_c_mangled_container_path_intern( & mut mangled_type, Self :: path_to_generic_args( & $p_arg. path) ,
1808
- & subtype, is_ref, is_mut, ptr_for_ref, true ) ;
1803
+ generics , & subtype, is_ref, is_mut, ptr_for_ref, true ) ;
1809
1804
if let Some ( w2) = $extra_write as Option <& mut Vec <u8 >> {
1810
1805
self . write_c_mangled_container_path_intern( w2, Self :: path_to_generic_args( & $p_arg. path) ,
1811
- & subtype, is_ref, is_mut, ptr_for_ref, true ) ;
1806
+ generics , & subtype, is_ref, is_mut, ptr_for_ref, true ) ;
1812
1807
}
1813
1808
} else if let Some ( id) = single_ident_generic_path_to_ident( & $p_arg. path) {
1814
1809
write!( w, "{}" , id) . unwrap( ) ;
@@ -1842,7 +1837,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1842
1837
write ! ( mangled_type, "Z" ) . unwrap ( ) ;
1843
1838
write ! ( mangled_tuple_type, "Z" ) . unwrap ( ) ;
1844
1839
self . check_create_container ( String :: from_utf8 ( mangled_tuple_type) . unwrap ( ) ,
1845
- & format ! ( "{}Tuple" , tuple. elems. len( ) ) , tuple. elems . iter ( ) . collect ( ) , is_ref) ;
1840
+ & format ! ( "{}Tuple" , tuple. elems. len( ) ) , tuple. elems . iter ( ) . collect ( ) , generics , is_ref) ;
1846
1841
}
1847
1842
} else if let syn:: Type :: Path ( p_arg) = arg {
1848
1843
write_path ! ( p_arg, None ) ;
@@ -1876,23 +1871,22 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1876
1871
write ! ( mangled_type, "Z" ) . unwrap ( ) ;
1877
1872
1878
1873
// Make sure the type is actually defined:
1879
- self . check_create_container ( String :: from_utf8 ( mangled_type) . unwrap ( ) , ident, args, is_ref) ;
1874
+ self . check_create_container ( String :: from_utf8 ( mangled_type) . unwrap ( ) , ident, args, generics , is_ref) ;
1880
1875
true
1881
1876
}
1882
- fn write_c_mangled_container_path < W : std:: io:: Write > ( & mut self , w : & mut W , args : Vec < & syn:: Type > , ident : & str , is_ref : bool , is_mut : bool , ptr_for_ref : bool ) -> bool {
1877
+ fn write_c_mangled_container_path < W : std:: io:: Write > ( & mut self , w : & mut W , args : Vec < & syn:: Type > , generics : Option < & GenericTypes > , ident : & str , is_ref : bool , is_mut : bool , ptr_for_ref : bool ) -> bool {
1883
1878
if !self . is_transparent_container ( ident, is_ref) {
1884
1879
write ! ( w, "{}::" , Self :: generated_container_path( ) ) . unwrap ( ) ;
1885
1880
}
1886
- self . write_c_mangled_container_path_intern ( w, args, ident, is_ref, is_mut, ptr_for_ref, false )
1881
+ self . write_c_mangled_container_path_intern ( w, args, generics , ident, is_ref, is_mut, ptr_for_ref, false )
1887
1882
}
1888
1883
1889
1884
// **********************************
1890
1885
// *** C Type Equivalent Printing ***
1891
1886
// **********************************
1892
1887
1893
- fn write_c_path_intern < W : std:: io:: Write > ( & self , w : & mut W , path : & syn:: Path , is_ref : bool , is_mut : bool , ptr_for_ref : bool ) -> bool {
1894
- //eprintln!("pcpi ({} {} {}): {:?}", is_ref, is_mut, ptr_for_ref, path);
1895
- let full_path = match self . maybe_resolve_path ( & path) {
1888
+ fn write_c_path_intern < W : std:: io:: Write > ( & self , w : & mut W , path : & syn:: Path , generics : Option < & GenericTypes > , is_ref : bool , is_mut : bool , ptr_for_ref : bool ) -> bool {
1889
+ let full_path = match self . maybe_resolve_path ( & path, generics) {
1896
1890
Some ( path) => path, None => return false } ;
1897
1891
if let Some ( c_type) = self . c_type_from_path ( & full_path, is_ref, ptr_for_ref) {
1898
1892
write ! ( w, "{}" , c_type) . unwrap ( ) ;
@@ -1923,43 +1917,36 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1923
1917
false
1924
1918
}
1925
1919
}
1926
- fn write_c_type_intern < W : std:: io:: Write > ( & mut self , generics : Option < & GenericTypes > , w : & mut W , t : & syn:: Type , is_ref : bool , is_mut : bool , ptr_for_ref : bool ) -> bool {
1920
+ fn write_c_type_intern < W : std:: io:: Write > ( & mut self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > , is_ref : bool , is_mut : bool , ptr_for_ref : bool ) -> bool {
1927
1921
match t {
1928
1922
syn:: Type :: Path ( p) => {
1929
1923
if p. qself . is_some ( ) || p. path . leading_colon . is_some ( ) {
1930
1924
return false ;
1931
1925
}
1932
- if let Some ( gen_types) = generics {
1933
- if let Some ( resolved) = gen_types. maybe_resolve_path ( & p. path ) {
1934
- if self . is_known_container ( & resolved. 0 , is_ref) { return false ; }
1935
- if self . is_transparent_container ( & resolved. 0 , is_ref) { return false ; }
1936
- return self . write_c_path_intern ( w, & resolved. 1 , is_ref, is_mut, ptr_for_ref) ;
1937
- }
1938
- }
1939
- if let Some ( full_path) = self . maybe_resolve_path ( & p. path ) {
1926
+ if let Some ( full_path) = self . maybe_resolve_path ( & p. path , generics) {
1940
1927
if self . is_known_container ( & full_path, is_ref) || self . is_transparent_container ( & full_path, is_ref) {
1941
- return self . write_c_mangled_container_path ( w, Self :: path_to_generic_args ( & p. path ) , & full_path, is_ref, is_mut, ptr_for_ref) ;
1928
+ return self . write_c_mangled_container_path ( w, Self :: path_to_generic_args ( & p. path ) , generics , & full_path, is_ref, is_mut, ptr_for_ref) ;
1942
1929
}
1943
1930
}
1944
1931
if p. path . leading_colon . is_some ( ) { return false ; }
1945
- self . write_c_path_intern ( w, & p. path , is_ref, is_mut, ptr_for_ref)
1932
+ self . write_c_path_intern ( w, & p. path , generics , is_ref, is_mut, ptr_for_ref)
1946
1933
} ,
1947
1934
syn:: Type :: Reference ( r) => {
1948
1935
if let Some ( lft) = & r. lifetime {
1949
1936
if format ! ( "{}" , lft. ident) != "static" { return false ; }
1950
1937
}
1951
- self . write_c_type_intern ( generics , w, & * r. elem , true , r. mutability . is_some ( ) , ptr_for_ref)
1938
+ self . write_c_type_intern ( w, & * r. elem , generics , true , r. mutability . is_some ( ) , ptr_for_ref)
1952
1939
} ,
1953
1940
syn:: Type :: Array ( a) => {
1954
1941
if is_ref && is_mut {
1955
1942
write ! ( w, "*mut [" ) . unwrap ( ) ;
1956
- if !self . write_c_type_intern ( generics , w, & a. elem , false , false , ptr_for_ref) { return false ; }
1943
+ if !self . write_c_type_intern ( w, & a. elem , generics , false , false , ptr_for_ref) { return false ; }
1957
1944
} else if is_ref {
1958
1945
write ! ( w, "*const [" ) . unwrap ( ) ;
1959
- if !self . write_c_type_intern ( generics , w, & a. elem , false , false , ptr_for_ref) { return false ; }
1946
+ if !self . write_c_type_intern ( w, & a. elem , generics , false , false , ptr_for_ref) { return false ; }
1960
1947
} else {
1961
1948
let mut typecheck = Vec :: new ( ) ;
1962
- if !self . write_c_type_intern ( generics , & mut typecheck, & a. elem , false , false , ptr_for_ref) { return false ; }
1949
+ if !self . write_c_type_intern ( & mut typecheck, & a. elem , generics , false , false , ptr_for_ref) { return false ; }
1963
1950
if typecheck[ ..] != [ 'u' as u8 , '8' as u8 ] { return false ; }
1964
1951
}
1965
1952
if let syn:: Expr :: Lit ( l) = & a. len {
@@ -1996,7 +1983,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
1996
1983
format ! ( "CVec_{}Z" , id)
1997
1984
} else { return false ; } ;
1998
1985
write ! ( w, "{}::{}" , Self :: generated_container_path( ) , mangled_container) . unwrap ( ) ;
1999
- self . check_create_container ( mangled_container, "Vec" , vec ! [ & * r. elem] , false ) ;
1986
+ self . check_create_container ( mangled_container, "Vec" , vec ! [ & * r. elem] , generics , false ) ;
2000
1987
true
2001
1988
} else { false }
2002
1989
} else { false }
@@ -2005,21 +1992,21 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
2005
1992
if t. elems . len ( ) == 0 {
2006
1993
true
2007
1994
} else {
2008
- self . write_c_mangled_container_path ( w, t. elems . iter ( ) . collect ( ) ,
1995
+ self . write_c_mangled_container_path ( w, t. elems . iter ( ) . collect ( ) , generics ,
2009
1996
& format ! ( "{}Tuple" , t. elems. len( ) ) , is_ref, is_mut, ptr_for_ref)
2010
1997
}
2011
1998
} ,
2012
1999
_ => false ,
2013
2000
}
2014
2001
}
2015
2002
pub fn write_c_type < W : std:: io:: Write > ( & mut self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > , ptr_for_ref : bool ) {
2016
- assert ! ( self . write_c_type_intern( generics , w, t, false , false , ptr_for_ref) ) ;
2003
+ assert ! ( self . write_c_type_intern( w, t, generics , false , false , ptr_for_ref) ) ;
2017
2004
}
2018
2005
pub fn understood_c_path ( & mut self , p : & syn:: Path ) -> bool {
2019
2006
if p. leading_colon . is_some ( ) { return false ; }
2020
- self . write_c_path_intern ( & mut std:: io:: sink ( ) , p, false , false , false )
2007
+ self . write_c_path_intern ( & mut std:: io:: sink ( ) , p, None , false , false , false )
2021
2008
}
2022
2009
pub fn understood_c_type ( & mut self , t : & syn:: Type , generics : Option < & GenericTypes > ) -> bool {
2023
- self . write_c_type_intern ( generics , & mut std:: io:: sink ( ) , t, false , false , false )
2010
+ self . write_c_type_intern ( & mut std:: io:: sink ( ) , t, generics , false , false , false )
2024
2011
}
2025
2012
}
0 commit comments