Gen bindings with rustc --pretty=expanded instead of reading files
[rust-lightning] / c-bindings-gen / src / types.rs
1 use std::collections::{HashMap, HashSet};
2 use std::fs::File;
3 use std::io::Write;
4 use std::hash;
5
6 use crate::blocks::*;
7
8 use proc_macro2::{TokenTree, Span};
9
10 // The following utils are used purely to build our known types maps - they break down all the
11 // types we need to resolve to include the given object, and no more.
12
13 pub fn first_seg_self<'a>(t: &'a syn::Type) -> Option<impl Iterator<Item=&syn::PathSegment> + 'a> {
14         match t {
15                 syn::Type::Path(p) => {
16                         if p.qself.is_some() || p.path.leading_colon.is_some() {
17                                 return None;
18                         }
19                         let mut segs = p.path.segments.iter();
20                         let ty = segs.next().unwrap();
21                         if !ty.arguments.is_empty() { return None; }
22                         if format!("{}", ty.ident) == "Self" {
23                                 Some(segs)
24                         } else { None }
25                 },
26                 _ => None,
27         }
28 }
29
30 pub fn get_single_remaining_path_seg<'a, I: Iterator<Item=&'a syn::PathSegment>>(segs: &mut I) -> Option<&'a syn::Ident> {
31         if let Some(ty) = segs.next() {
32                 if !ty.arguments.is_empty() { unimplemented!(); }
33                 if segs.next().is_some() { return None; }
34                 Some(&ty.ident)
35         } else { None }
36 }
37
38 pub fn single_ident_generic_path_to_ident(p: &syn::Path) -> Option<&syn::Ident> {
39         if p.segments.len() == 1 {
40                 Some(&p.segments.iter().next().unwrap().ident)
41         } else { None }
42 }
43
44 pub fn path_matches_nongeneric(p: &syn::Path, exp: &[&str]) -> bool {
45         if p.segments.len() != exp.len() { return false; }
46         for (seg, e) in p.segments.iter().zip(exp.iter()) {
47                 if seg.arguments != syn::PathArguments::None { return false; }
48                 if &format!("{}", seg.ident) != *e { return false; }
49         }
50         true
51 }
52
53 #[derive(Debug, PartialEq)]
54 pub enum ExportStatus {
55         Export,
56         NoExport,
57         TestOnly,
58 }
59 /// Gets the ExportStatus of an object (struct, fn, etc) given its attributes.
60 pub fn export_status(attrs: &[syn::Attribute]) -> ExportStatus {
61         for attr in attrs.iter() {
62                 let tokens_clone = attr.tokens.clone();
63                 let mut token_iter = tokens_clone.into_iter();
64                 if let Some(token) = token_iter.next() {
65                         match token {
66                                 TokenTree::Punct(c) if c.as_char() == '=' => {
67                                         // Really not sure where syn gets '=' from here -
68                                         // it somehow represents '///' or '//!'
69                                 },
70                                 TokenTree::Group(g) => {
71                                         if format!("{}", single_ident_generic_path_to_ident(&attr.path).unwrap()) == "cfg" {
72                                                 let mut iter = g.stream().into_iter();
73                                                 if let TokenTree::Ident(i) = iter.next().unwrap() {
74                                                         if i == "any" {
75                                                                 // #[cfg(any(test, feature = ""))]
76                                                                 if let TokenTree::Group(g) = iter.next().unwrap() {
77                                                                         if let TokenTree::Ident(i) = g.stream().into_iter().next().unwrap() {
78                                                                                 if i == "test" || i == "feature" {
79                                                                                         // If its cfg(feature(...)) we assume its test-only
80                                                                                         return ExportStatus::TestOnly;
81                                                                                 }
82                                                                         }
83                                                                 }
84                                                         } else if i == "test" || i == "feature" {
85                                                                 // If its cfg(feature(...)) we assume its test-only
86                                                                 return ExportStatus::TestOnly;
87                                                         }
88                                                 }
89                                         }
90                                         continue; // eg #[derive()]
91                                 },
92                                 _ => unimplemented!(),
93                         }
94                 } else { continue; }
95                 match token_iter.next().unwrap() {
96                         TokenTree::Literal(lit) => {
97                                 let line = format!("{}", lit);
98                                 if line.contains("(C-not exported)") {
99                                         return ExportStatus::NoExport;
100                                 }
101                         },
102                         _ => unimplemented!(),
103                 }
104         }
105         ExportStatus::Export
106 }
107
108 pub fn assert_simple_bound(bound: &syn::TraitBound) {
109         if bound.paren_token.is_some() || bound.lifetimes.is_some() { unimplemented!(); }
110         if let syn::TraitBoundModifier::Maybe(_) = bound.modifier { unimplemented!(); }
111 }
112
113 /// A stack of sets of generic resolutions.
114 ///
115 /// This tracks the template parameters for a function, struct, or trait, allowing resolution into
116 /// a concrete type. By pushing a new context onto the stack, this can track a function's template
117 /// parameters inside of a generic struct or trait.
118 ///
119 /// It maps both direct types as well as Deref<Target = X>, mapping them via the provided
120 /// TypeResolver's resolve_path function (ie traits map to the concrete jump table, structs to the
121 /// concrete C container struct, etc).
122 pub struct GenericTypes<'a> {
123         typed_generics: Vec<HashMap<&'a syn::Ident, (String, Option<&'a syn::Path>)>>,
124 }
125 impl<'a> GenericTypes<'a> {
126         pub fn new() -> Self {
127                 Self { typed_generics: vec![HashMap::new()], }
128         }
129
130         /// push a new context onto the stack, allowing for a new set of generics to be learned which
131         /// will override any lower contexts, but which will still fall back to resoltion via lower
132         /// contexts.
133         pub fn push_ctx(&mut self) {
134                 self.typed_generics.push(HashMap::new());
135         }
136         /// pop the latest context off the stack.
137         pub fn pop_ctx(&mut self) {
138                 self.typed_generics.pop();
139         }
140
141         /// Learn the generics in generics in the current context, given a TypeResolver.
142         pub fn learn_generics<'b, 'c>(&mut self, generics: &'a syn::Generics, types: &'b TypeResolver<'a, 'c>) -> bool {
143                 // First learn simple generics...
144                 for generic in generics.params.iter() {
145                         match generic {
146                                 syn::GenericParam::Type(type_param) => {
147                                         let mut non_lifetimes_processed = false;
148                                         for bound in type_param.bounds.iter() {
149                                                 if let syn::TypeParamBound::Trait(trait_bound) = bound {
150                                                         if let Some(ident) = single_ident_generic_path_to_ident(&trait_bound.path) {
151                                                                 match &format!("{}", ident) as &str { "Send" => continue, "Sync" => continue, _ => {} }
152                                                         }
153                                                         if path_matches_nongeneric(&trait_bound.path, &["core", "clone", "Clone"]) { continue; }
154
155                                                         assert_simple_bound(&trait_bound);
156                                                         if let Some(mut path) = types.maybe_resolve_path(&trait_bound.path, None) {
157                                                                 if types.skip_path(&path) { continue; }
158                                                                 if non_lifetimes_processed { return false; }
159                                                                 non_lifetimes_processed = true;
160                                                                 let new_ident = if path != "std::ops::Deref" {
161                                                                         path = "crate::".to_string() + &path;
162                                                                         Some(&trait_bound.path)
163                                                                 } else { None };
164                                                                 self.typed_generics.last_mut().unwrap().insert(&type_param.ident, (path, new_ident));
165                                                         } else { return false; }
166                                                 }
167                                         }
168                                 },
169                                 _ => {},
170                         }
171                 }
172                 // Then find generics where we are required to pass a Deref<Target=X> and pretend its just X.
173                 if let Some(wh) = &generics.where_clause {
174                         for pred in wh.predicates.iter() {
175                                 if let syn::WherePredicate::Type(t) = pred {
176                                         if let syn::Type::Path(p) = &t.bounded_ty {
177                                                 if p.qself.is_some() { return false; }
178                                                 if p.path.leading_colon.is_some() { return false; }
179                                                 let mut p_iter = p.path.segments.iter();
180                                                 if let Some(gen) = self.typed_generics.last_mut().unwrap().get_mut(&p_iter.next().unwrap().ident) {
181                                                         if gen.0 != "std::ops::Deref" { return false; }
182                                                         if &format!("{}", p_iter.next().unwrap().ident) != "Target" { return false; }
183
184                                                         let mut non_lifetimes_processed = false;
185                                                         for bound in t.bounds.iter() {
186                                                                 if let syn::TypeParamBound::Trait(trait_bound) = bound {
187                                                                         if non_lifetimes_processed { return false; }
188                                                                         non_lifetimes_processed = true;
189                                                                         assert_simple_bound(&trait_bound);
190                                                                         *gen = ("crate::".to_string() + &types.resolve_path(&trait_bound.path, None),
191                                                                                 Some(&trait_bound.path));
192                                                                 }
193                                                         }
194                                                 } else { return false; }
195                                         } else { return false; }
196                                 }
197                         }
198                 }
199                 for (_, (_, ident)) in self.typed_generics.last().unwrap().iter() {
200                         if ident.is_none() { return false; }
201                 }
202                 true
203         }
204
205         /// Learn the associated types from the trait in the current context.
206         pub fn learn_associated_types<'b, 'c>(&mut self, t: &'a syn::ItemTrait, types: &'b TypeResolver<'a, 'c>) {
207                 for item in t.items.iter() {
208                         match item {
209                                 &syn::TraitItem::Type(ref t) => {
210                                         if t.default.is_some() || t.generics.lt_token.is_some() { unimplemented!(); }
211                                         let mut bounds_iter = t.bounds.iter();
212                                         match bounds_iter.next().unwrap() {
213                                                 syn::TypeParamBound::Trait(tr) => {
214                                                         assert_simple_bound(&tr);
215                                                         if let Some(mut path) = types.maybe_resolve_path(&tr.path, None) {
216                                                                 if types.skip_path(&path) { continue; }
217                                                                 // In general we handle Deref<Target=X> as if it were just X (and
218                                                                 // implement Deref<Target=Self> for relevant types). We don't
219                                                                 // bother to implement it for associated types, however, so we just
220                                                                 // ignore such bounds.
221                                                                 let new_ident = if path != "std::ops::Deref" {
222                                                                         path = "crate::".to_string() + &path;
223                                                                         Some(&tr.path)
224                                                                 } else { None };
225                                                                 self.typed_generics.last_mut().unwrap().insert(&t.ident, (path, new_ident));
226                                                         } else { unimplemented!(); }
227                                                 },
228                                                 _ => unimplemented!(),
229                                         }
230                                         if bounds_iter.next().is_some() { unimplemented!(); }
231                                 },
232                                 _ => {},
233                         }
234                 }
235         }
236
237         /// Attempt to resolve an Ident as a generic parameter and return the full path.
238         pub fn maybe_resolve_ident<'b>(&'b self, ident: &syn::Ident) -> Option<&'b String> {
239                 for gen in self.typed_generics.iter().rev() {
240                         if let Some(res) = gen.get(ident).map(|(a, _)| a) {
241                                 return Some(res);
242                         }
243                 }
244                 None
245         }
246         /// Attempt to resolve a Path as a generic parameter and return the full path. as both a string
247         /// and syn::Path.
248         pub fn maybe_resolve_path<'b>(&'b self, path: &syn::Path) -> Option<(&'b String, &'a syn::Path)> {
249                 if let Some(ident) = path.get_ident() {
250                         for gen in self.typed_generics.iter().rev() {
251                                 if let Some(res) = gen.get(ident).map(|(a, b)| (a, b.unwrap())) {
252                                         return Some(res);
253                                 }
254                         }
255                 } else {
256                         // Associated types are usually specified as "Self::Generic", so we check for that
257                         // explicitly here.
258                         let mut it = path.segments.iter();
259                         if path.segments.len() == 2 && format!("{}", it.next().unwrap().ident) == "Self" {
260                                 let ident = &it.next().unwrap().ident;
261                                 for gen in self.typed_generics.iter().rev() {
262                                         if let Some(res) = gen.get(ident).map(|(a, b)| (a, b.unwrap())) {
263                                                 return Some(res);
264                                         }
265                                 }
266                         }
267                 }
268                 None
269         }
270 }
271
272 #[derive(Clone, PartialEq)]
273 // The type of declaration and the object itself
274 pub enum DeclType<'a> {
275         MirroredEnum,
276         Trait(&'a syn::ItemTrait),
277         StructImported,
278         StructIgnored,
279         EnumIgnored,
280 }
281
282 // templates_defined is walked to write the C++ header, so if we use the default hashing it get
283 // reordered on each genbindings run. Instead, we use SipHasher (which defaults to 0-keys) so that
284 // the sorting is stable across runs. It is deprecated, but the "replacement" doesn't actually
285 // accomplish the same goals, so we just ignore it.
286 #[allow(deprecated)]
287 pub type NonRandomHash = hash::BuildHasherDefault<hash::SipHasher>;
288
289 /// Top-level struct tracking everything which has been defined while walking the crate.
290 pub struct CrateTypes<'a> {
291         /// This may contain structs or enums, but only when either is mapped as
292         /// struct X { inner: *mut originalX, .. }
293         pub opaques: HashMap<String, &'a syn::Ident>,
294         /// Enums which are mapped as C enums with conversion functions
295         pub mirrored_enums: HashMap<String, &'a syn::ItemEnum>,
296         /// Traits which are mapped as a pointer + jump table
297         pub traits: HashMap<String, &'a syn::ItemTrait>,
298         /// Aliases from paths to some other Type
299         pub type_aliases: HashMap<String, syn::Type>,
300         /// Template continer types defined, map from mangled type name -> whether a destructor fn
301         /// exists.
302         ///
303         /// This is used at the end of processing to make C++ wrapper classes
304         pub templates_defined: HashMap<String, bool, NonRandomHash>,
305         /// The output file for any created template container types, written to as we find new
306         /// template containers which need to be defined.
307         pub template_file: &'a mut File,
308         /// Set of containers which are clonable
309         pub clonable_types: HashSet<String>,
310 }
311
312 /// A struct which tracks resolving rust types into C-mapped equivalents, exists for one specific
313 /// module but contains a reference to the overall CrateTypes tracking.
314 pub struct TypeResolver<'mod_lifetime, 'crate_lft: 'mod_lifetime> {
315         pub orig_crate: &'mod_lifetime str,
316         pub module_path: &'mod_lifetime str,
317         imports: HashMap<syn::Ident, String>,
318         // ident -> is-mirrored-enum
319         declared: HashMap<syn::Ident, DeclType<'crate_lft>>,
320         pub crate_types: &'mod_lifetime mut CrateTypes<'crate_lft>,
321 }
322
323 /// Returned by write_empty_rust_val_check_suffix to indicate what type of dereferencing needs to
324 /// happen to get the inner value of a generic.
325 enum EmptyValExpectedTy {
326         /// A type which has a flag for being empty (eg an array where we treat all-0s as empty).
327         NonPointer,
328         /// A pointer that we want to dereference and move out of.
329         OwnedPointer,
330         /// A pointer which we want to convert to a reference.
331         ReferenceAsPointer,
332 }
333
334 impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
335         pub fn new(orig_crate: &'a str, module_path: &'a str, crate_types: &'a mut CrateTypes<'c>) -> Self {
336                 let mut imports = HashMap::new();
337                 // Add primitives to the "imports" list:
338                 imports.insert(syn::Ident::new("bool", Span::call_site()), "bool".to_string());
339                 imports.insert(syn::Ident::new("u64", Span::call_site()), "u64".to_string());
340                 imports.insert(syn::Ident::new("u32", Span::call_site()), "u32".to_string());
341                 imports.insert(syn::Ident::new("u16", Span::call_site()), "u16".to_string());
342                 imports.insert(syn::Ident::new("u8", Span::call_site()), "u8".to_string());
343                 imports.insert(syn::Ident::new("usize", Span::call_site()), "usize".to_string());
344                 imports.insert(syn::Ident::new("str", Span::call_site()), "str".to_string());
345                 imports.insert(syn::Ident::new("String", Span::call_site()), "String".to_string());
346
347                 // These are here to allow us to print native Rust types in trait fn impls even if we don't
348                 // have C mappings:
349                 imports.insert(syn::Ident::new("Result", Span::call_site()), "Result".to_string());
350                 imports.insert(syn::Ident::new("Vec", Span::call_site()), "Vec".to_string());
351                 imports.insert(syn::Ident::new("Option", Span::call_site()), "Option".to_string());
352                 Self { orig_crate, module_path, imports, declared: HashMap::new(), crate_types }
353         }
354
355         // *************************************************
356         // *** Well know type and conversion definitions ***
357         // *************************************************
358
359         /// Returns true we if can just skip passing this to C entirely
360         fn skip_path(&self, full_path: &str) -> bool {
361                 full_path == "bitcoin::secp256k1::Secp256k1" ||
362                 full_path == "bitcoin::secp256k1::Signing" ||
363                 full_path == "bitcoin::secp256k1::Verification"
364         }
365         /// Returns true we if can just skip passing this to C entirely
366         fn no_arg_path_to_rust(&self, full_path: &str) -> &str {
367                 if full_path == "bitcoin::secp256k1::Secp256k1" {
368                         "&bitcoin::secp256k1::Secp256k1::new()"
369                 } else { unimplemented!(); }
370         }
371
372         /// Returns true if the object is a primitive and is mapped as-is with no conversion
373         /// whatsoever.
374         pub fn is_primitive(&self, full_path: &str) -> bool {
375                 match full_path {
376                         "bool" => true,
377                         "u64" => true,
378                         "u32" => true,
379                         "u16" => true,
380                         "u8" => true,
381                         "usize" => true,
382                         _ => false,
383                 }
384         }
385         pub fn is_clonable(&self, ty: &str) -> bool {
386                 if self.crate_types.clonable_types.contains(ty) { return true; }
387                 if self.is_primitive(ty) { return true; }
388                 match ty {
389                         "()" => true,
390                         "crate::c_types::Signature" => true,
391                         "crate::c_types::TxOut" => true,
392                         _ => false,
393                 }
394         }
395         /// Gets the C-mapped type for types which are outside of the crate, or which are manually
396         /// ignored by for some reason need mapping anyway.
397         fn c_type_from_path<'b>(&self, full_path: &'b str, is_ref: bool, ptr_for_ref: bool) -> Option<&'b str> {
398                 if self.is_primitive(full_path) {
399                         return Some(full_path);
400                 }
401                 match full_path {
402                         "Result" => Some("crate::c_types::derived::CResult"),
403                         "Vec" if !is_ref => Some("crate::c_types::derived::CVec"),
404                         "Option" => Some(""),
405
406                         // Note that no !is_ref types can map to an array because Rust and C's call semantics
407                         // for arrays are different (https://github.com/eqrion/cbindgen/issues/528)
408
409                         "[u8; 32]" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
410                         "[u8; 16]" if !is_ref => Some("crate::c_types::SixteenBytes"),
411                         "[u8; 10]" if !is_ref => Some("crate::c_types::TenBytes"),
412                         "[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes"),
413                         "[u8; 3]" if !is_ref => Some("crate::c_types::ThreeBytes"), // Used for RGB values
414
415                         "str" if is_ref => Some("crate::c_types::Str"),
416                         "String" if !is_ref => Some("crate::c_types::derived::CVec_u8Z"),
417                         "String" if is_ref => Some("crate::c_types::Str"),
418
419                         "std::time::Duration" => Some("u64"),
420
421                         "bitcoin::secp256k1::key::PublicKey" => Some("crate::c_types::PublicKey"),
422                         "bitcoin::secp256k1::Signature" => Some("crate::c_types::Signature"),
423                         "bitcoin::secp256k1::key::SecretKey" if is_ref  => Some("*const [u8; 32]"),
424                         "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some("crate::c_types::SecretKey"),
425                         "bitcoin::secp256k1::Error" if !is_ref => Some("crate::c_types::Secp256k1Error"),
426                         "bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice"),
427                         "bitcoin::blockdata::script::Script" if !is_ref => Some("crate::c_types::derived::CVec_u8Z"),
428                         "bitcoin::blockdata::transaction::OutPoint" => Some("crate::chain::transaction::OutPoint"),
429                         "bitcoin::blockdata::transaction::Transaction" => Some("crate::c_types::Transaction"),
430                         "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some("crate::c_types::TxOut"),
431                         "bitcoin::network::constants::Network" => Some("crate::bitcoin::network::Network"),
432                         "bitcoin::blockdata::block::BlockHeader" if is_ref  => Some("*const [u8; 80]"),
433                         "bitcoin::blockdata::block::Block" if is_ref  => Some("crate::c_types::u8slice"),
434
435                         // Newtypes that we just expose in their original form.
436                         "bitcoin::hash_types::Txid" if is_ref  => Some("*const [u8; 32]"),
437                         "bitcoin::hash_types::Txid" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
438                         "bitcoin::hash_types::BlockHash" if is_ref  => Some("*const [u8; 32]"),
439                         "bitcoin::hash_types::BlockHash" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
440                         "bitcoin::secp256k1::Message" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
441                         "ln::channelmanager::PaymentHash" if is_ref => Some("*const [u8; 32]"),
442                         "ln::channelmanager::PaymentHash" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
443                         "ln::channelmanager::PaymentPreimage" if is_ref => Some("*const [u8; 32]"),
444                         "ln::channelmanager::PaymentPreimage" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
445                         "ln::channelmanager::PaymentSecret" if is_ref => Some("crate::c_types::ThirtyTwoBytes"),
446                         "ln::channelmanager::PaymentSecret" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
447
448                         // Override the default since Records contain an fmt with a lifetime:
449                         "util::logger::Record" => Some("*const std::os::raw::c_char"),
450
451                         // List of structs we map that aren't detected:
452                         "ln::features::InitFeatures" if is_ref && ptr_for_ref => Some("crate::ln::features::InitFeatures"),
453                         "ln::features::InitFeatures" if is_ref => Some("*const crate::ln::features::InitFeatures"),
454                         "ln::features::InitFeatures" => Some("crate::ln::features::InitFeatures"),
455                         _ => None,
456                 }
457         }
458
459         fn from_c_conversion_new_var_from_path<'b>(&self, _full_path: &str, _is_ref: bool) -> Option<(&'b str, &'b str)> {
460                 None
461         }
462         fn from_c_conversion_prefix_from_path<'b>(&self, full_path: &str, is_ref: bool) -> Option<String> {
463                 if self.is_primitive(full_path) {
464                         return Some("".to_owned());
465                 }
466                 match full_path {
467                         "Vec" if !is_ref => Some("local_"),
468                         "Result" if !is_ref => Some("local_"),
469                         "Option" if is_ref => Some("&local_"),
470                         "Option" => Some("local_"),
471
472                         "[u8; 32]" if is_ref => Some("unsafe { &*"),
473                         "[u8; 32]" if !is_ref => Some(""),
474                         "[u8; 16]" if !is_ref => Some(""),
475                         "[u8; 10]" if !is_ref => Some(""),
476                         "[u8; 4]" if !is_ref => Some(""),
477                         "[u8; 3]" if !is_ref => Some(""),
478
479                         "[u8]" if is_ref => Some(""),
480                         "[usize]" if is_ref => Some(""),
481
482                         "str" if is_ref => Some(""),
483                         "String" if !is_ref => Some("String::from_utf8("),
484                         // Note that we'll panic for String if is_ref, as we only have non-owned memory, we
485                         // cannot create a &String.
486
487                         "std::time::Duration" => Some("std::time::Duration::from_secs("),
488
489                         "bitcoin::secp256k1::key::PublicKey" if is_ref => Some("&"),
490                         "bitcoin::secp256k1::key::PublicKey" => Some(""),
491                         "bitcoin::secp256k1::Signature" if is_ref => Some("&"),
492                         "bitcoin::secp256k1::Signature" => Some(""),
493                         "bitcoin::secp256k1::key::SecretKey" if is_ref => Some("&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *"),
494                         "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some(""),
495                         "bitcoin::blockdata::script::Script" if is_ref => Some("&::bitcoin::blockdata::script::Script::from(Vec::from("),
496                         "bitcoin::blockdata::script::Script" if !is_ref => Some("::bitcoin::blockdata::script::Script::from("),
497                         "bitcoin::blockdata::transaction::Transaction" if is_ref => Some("&"),
498                         "bitcoin::blockdata::transaction::Transaction" => Some(""),
499                         "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(""),
500                         "bitcoin::network::constants::Network" => Some(""),
501                         "bitcoin::blockdata::block::BlockHeader" => Some("&::bitcoin::consensus::encode::deserialize(unsafe { &*"),
502                         "bitcoin::blockdata::block::Block" if is_ref => Some("&::bitcoin::consensus::encode::deserialize("),
503
504                         // Newtypes that we just expose in their original form.
505                         "bitcoin::hash_types::Txid" if is_ref => Some("&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*"),
506                         "bitcoin::hash_types::Txid" if !is_ref => Some("::bitcoin::hash_types::Txid::from_slice(&"),
507                         "bitcoin::hash_types::BlockHash" => Some("::bitcoin::hash_types::BlockHash::from_slice(&"),
508                         "ln::channelmanager::PaymentHash" if !is_ref => Some("::lightning::ln::channelmanager::PaymentHash("),
509                         "ln::channelmanager::PaymentHash" if is_ref => Some("&::lightning::ln::channelmanager::PaymentHash(unsafe { *"),
510                         "ln::channelmanager::PaymentPreimage" if !is_ref => Some("::lightning::ln::channelmanager::PaymentPreimage("),
511                         "ln::channelmanager::PaymentPreimage" if is_ref => Some("&::lightning::ln::channelmanager::PaymentPreimage(unsafe { *"),
512                         "ln::channelmanager::PaymentSecret" => Some("::lightning::ln::channelmanager::PaymentSecret("),
513
514                         // List of structs we map (possibly during processing of other files):
515                         "ln::features::InitFeatures" if !is_ref => Some("*unsafe { Box::from_raw("),
516
517                         // List of traits we map (possibly during processing of other files):
518                         "crate::util::logger::Logger" => Some(""),
519
520                         _ => None,
521                 }.map(|s| s.to_owned())
522         }
523         fn from_c_conversion_suffix_from_path<'b>(&self, full_path: &str, is_ref: bool) -> Option<String> {
524                 if self.is_primitive(full_path) {
525                         return Some("".to_owned());
526                 }
527                 match full_path {
528                         "Vec" if !is_ref => Some(""),
529                         "Option" => Some(""),
530                         "Result" if !is_ref => Some(""),
531
532                         "[u8; 32]" if is_ref => Some("}"),
533                         "[u8; 32]" if !is_ref => Some(".data"),
534                         "[u8; 16]" if !is_ref => Some(".data"),
535                         "[u8; 10]" if !is_ref => Some(".data"),
536                         "[u8; 4]" if !is_ref => Some(".data"),
537                         "[u8; 3]" if !is_ref => Some(".data"),
538
539                         "[u8]" if is_ref => Some(".to_slice()"),
540                         "[usize]" if is_ref => Some(".to_slice()"),
541
542                         "str" if is_ref => Some(".into()"),
543                         "String" if !is_ref => Some(".into_rust()).unwrap()"),
544
545                         "std::time::Duration" => Some(")"),
546
547                         "bitcoin::secp256k1::key::PublicKey" => Some(".into_rust()"),
548                         "bitcoin::secp256k1::Signature" => Some(".into_rust()"),
549                         "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some(".into_rust()"),
550                         "bitcoin::secp256k1::key::SecretKey" if is_ref => Some("}[..]).unwrap()"),
551                         "bitcoin::blockdata::script::Script" if is_ref => Some(".to_slice()))"),
552                         "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_rust())"),
553                         "bitcoin::blockdata::transaction::Transaction" => Some(".into_bitcoin()"),
554                         "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(".into_rust()"),
555                         "bitcoin::network::constants::Network" => Some(".into_bitcoin()"),
556                         "bitcoin::blockdata::block::BlockHeader" => Some(" }).unwrap()"),
557                         "bitcoin::blockdata::block::Block" => Some(".to_slice()).unwrap()"),
558
559                         // Newtypes that we just expose in their original form.
560                         "bitcoin::hash_types::Txid" if is_ref => Some(" }[..]).unwrap()"),
561                         "bitcoin::hash_types::Txid" => Some(".data[..]).unwrap()"),
562                         "bitcoin::hash_types::BlockHash" if !is_ref => Some(".data[..]).unwrap()"),
563                         "ln::channelmanager::PaymentHash" if !is_ref => Some(".data)"),
564                         "ln::channelmanager::PaymentHash" if is_ref => Some(" })"),
565                         "ln::channelmanager::PaymentPreimage" if !is_ref => Some(".data)"),
566                         "ln::channelmanager::PaymentPreimage" if is_ref => Some(" })"),
567                         "ln::channelmanager::PaymentSecret" => Some(".data)"),
568
569                         // List of structs we map (possibly during processing of other files):
570                         "ln::features::InitFeatures" if is_ref => Some(".inner) }"),
571                         "ln::features::InitFeatures" if !is_ref => Some(".take_inner()) }"),
572
573                         // List of traits we map (possibly during processing of other files):
574                         "crate::util::logger::Logger" => Some(""),
575
576                         _ => None,
577                 }.map(|s| s.to_owned())
578         }
579
580         fn to_c_conversion_new_var_from_path<'b>(&self, full_path: &str, is_ref: bool) -> Option<(&'b str, &'b str)> {
581                 if self.is_primitive(full_path) {
582                         return None;
583                 }
584                 match full_path {
585                         "[u8]" if is_ref => Some(("crate::c_types::u8slice::from_slice(", ")")),
586                         "[usize]" if is_ref => Some(("crate::c_types::usizeslice::from_slice(", ")")),
587
588                         "bitcoin::blockdata::transaction::Transaction" if is_ref => Some(("::bitcoin::consensus::encode::serialize(", ")")),
589                         "bitcoin::blockdata::transaction::Transaction" if !is_ref => Some(("::bitcoin::consensus::encode::serialize(&", ")")),
590                         "bitcoin::blockdata::block::BlockHeader" if is_ref => Some(("{ let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(", ")); s }")),
591                         "bitcoin::blockdata::block::Block" if is_ref => Some(("::bitcoin::consensus::encode::serialize(", ")")),
592                         "bitcoin::hash_types::Txid" => None,
593
594                         // Override the default since Records contain an fmt with a lifetime:
595                         // TODO: We should include the other record fields
596                         "util::logger::Record" => Some(("std::ffi::CString::new(format!(\"{}\", ", ".args)).unwrap()")),
597                         _ => None,
598                 }.map(|s| s.to_owned())
599         }
600         fn to_c_conversion_inline_prefix_from_path(&self, full_path: &str, is_ref: bool, ptr_for_ref: bool) -> Option<String> {
601                 if self.is_primitive(full_path) {
602                         return Some("".to_owned());
603                 }
604                 match full_path {
605                         "Result" if !is_ref => Some("local_"),
606                         "Vec" if !is_ref => Some("local_"),
607                         "Option" => Some("local_"),
608
609                         "[u8; 32]" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
610                         "[u8; 32]" if is_ref => Some("&"),
611                         "[u8; 16]" if !is_ref => Some("crate::c_types::SixteenBytes { data: "),
612                         "[u8; 10]" if !is_ref => Some("crate::c_types::TenBytes { data: "),
613                         "[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes { data: "),
614                         "[u8; 3]" if is_ref => Some("&"),
615
616                         "[u8]" if is_ref => Some("local_"),
617                         "[usize]" if is_ref => Some("local_"),
618
619                         "str" if is_ref => Some(""),
620                         "String" => Some(""),
621
622                         "std::time::Duration" => Some(""),
623
624                         "bitcoin::secp256k1::key::PublicKey" => Some("crate::c_types::PublicKey::from_rust(&"),
625                         "bitcoin::secp256k1::Signature" => Some("crate::c_types::Signature::from_rust(&"),
626                         "bitcoin::secp256k1::key::SecretKey" if is_ref  => Some(""),
627                         "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some("crate::c_types::SecretKey::from_rust("),
628                         "bitcoin::secp256k1::Error" if !is_ref => Some("crate::c_types::Secp256k1Error::from_rust("),
629                         "bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice::from_slice(&"),
630                         "bitcoin::blockdata::script::Script" if !is_ref => Some(""),
631                         "bitcoin::blockdata::transaction::Transaction" => Some("crate::c_types::Transaction::from_vec(local_"),
632                         "bitcoin::blockdata::transaction::OutPoint" => Some("crate::c_types::bitcoin_to_C_outpoint("),
633                         "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some("crate::c_types::TxOut::from_rust("),
634                         "bitcoin::blockdata::block::BlockHeader" if is_ref => Some("&local_"),
635                         "bitcoin::blockdata::block::Block" if is_ref => Some("crate::c_types::u8slice::from_slice(&local_"),
636
637                         "bitcoin::hash_types::Txid" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
638
639                         // Newtypes that we just expose in their original form.
640                         "bitcoin::hash_types::Txid" if is_ref => Some(""),
641                         "bitcoin::hash_types::BlockHash" if is_ref => Some(""),
642                         "bitcoin::hash_types::BlockHash" => Some("crate::c_types::ThirtyTwoBytes { data: "),
643                         "bitcoin::secp256k1::Message" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
644                         "ln::channelmanager::PaymentHash" if is_ref => Some("&"),
645                         "ln::channelmanager::PaymentHash" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
646                         "ln::channelmanager::PaymentPreimage" if is_ref => Some("&"),
647                         "ln::channelmanager::PaymentPreimage" => Some("crate::c_types::ThirtyTwoBytes { data: "),
648                         "ln::channelmanager::PaymentSecret" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
649
650                         // Override the default since Records contain an fmt with a lifetime:
651                         "util::logger::Record" => Some("local_"),
652
653                         // List of structs we map (possibly during processing of other files):
654                         "ln::features::InitFeatures" if is_ref && ptr_for_ref => Some("crate::ln::features::InitFeatures { inner: &mut "),
655                         "ln::features::InitFeatures" if is_ref => Some("Box::into_raw(Box::new(crate::ln::features::InitFeatures { inner: &mut "),
656                         "ln::features::InitFeatures" if !is_ref => Some("crate::ln::features::InitFeatures { inner: Box::into_raw(Box::new("),
657
658                         _ => None,
659                 }.map(|s| s.to_owned())
660         }
661         fn to_c_conversion_inline_suffix_from_path(&self, full_path: &str, is_ref: bool, ptr_for_ref: bool) -> Option<String> {
662                 if self.is_primitive(full_path) {
663                         return Some("".to_owned());
664                 }
665                 match full_path {
666                         "Result" if !is_ref => Some(""),
667                         "Vec" if !is_ref => Some(".into()"),
668                         "Option" => Some(""),
669
670                         "[u8; 32]" if !is_ref => Some(" }"),
671                         "[u8; 32]" if is_ref => Some(""),
672                         "[u8; 16]" if !is_ref => Some(" }"),
673                         "[u8; 10]" if !is_ref => Some(" }"),
674                         "[u8; 4]" if !is_ref => Some(" }"),
675                         "[u8; 3]" if is_ref => Some(""),
676
677                         "[u8]" if is_ref => Some(""),
678                         "[usize]" if is_ref => Some(""),
679
680                         "str" if is_ref => Some(".into()"),
681                         "String" if !is_ref => Some(".into_bytes().into()"),
682                         "String" if is_ref => Some(".as_str().into()"),
683
684                         "std::time::Duration" => Some(".as_secs()"),
685
686                         "bitcoin::secp256k1::key::PublicKey" => Some(")"),
687                         "bitcoin::secp256k1::Signature" => Some(")"),
688                         "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some(")"),
689                         "bitcoin::secp256k1::key::SecretKey" if is_ref => Some(".as_ref()"),
690                         "bitcoin::secp256k1::Error" if !is_ref => Some(")"),
691                         "bitcoin::blockdata::script::Script" if is_ref => Some("[..])"),
692                         "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_bytes().into()"),
693                         "bitcoin::blockdata::transaction::Transaction" => Some(")"),
694                         "bitcoin::blockdata::transaction::OutPoint" => Some(")"),
695                         "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(")"),
696                         "bitcoin::blockdata::block::BlockHeader" if is_ref => Some(""),
697                         "bitcoin::blockdata::block::Block" if is_ref => Some(")"),
698
699                         "bitcoin::hash_types::Txid" if !is_ref => Some(".into_inner() }"),
700
701                         // Newtypes that we just expose in their original form.
702                         "bitcoin::hash_types::Txid" if is_ref => Some(".as_inner()"),
703                         "bitcoin::hash_types::BlockHash" if is_ref => Some(".as_inner()"),
704                         "bitcoin::hash_types::BlockHash" => Some(".into_inner() }"),
705                         "bitcoin::secp256k1::Message" if !is_ref => Some(".as_ref().clone() }"),
706                         "ln::channelmanager::PaymentHash" if is_ref => Some(".0"),
707                         "ln::channelmanager::PaymentHash" => Some(".0 }"),
708                         "ln::channelmanager::PaymentPreimage" if is_ref => Some(".0"),
709                         "ln::channelmanager::PaymentPreimage" => Some(".0 }"),
710                         "ln::channelmanager::PaymentSecret" if !is_ref => Some(".0 }"),
711
712                         // Override the default since Records contain an fmt with a lifetime:
713                         "util::logger::Record" => Some(".as_ptr()"),
714
715                         // List of structs we map (possibly during processing of other files):
716                         "ln::features::InitFeatures" if is_ref && ptr_for_ref => Some(", is_owned: false }"),
717                         "ln::features::InitFeatures" if is_ref => Some(", is_owned: false }))"),
718                         "ln::features::InitFeatures" => Some(")), is_owned: true }"),
719
720                         _ => None,
721                 }.map(|s| s.to_owned())
722         }
723
724         fn empty_val_check_suffix_from_path(&self, full_path: &str) -> Option<&str> {
725                 match full_path {
726                         "ln::channelmanager::PaymentSecret" => Some(".data == [0; 32]"),
727                         "bitcoin::secp256k1::key::PublicKey" => Some(".is_null()"),
728                         "bitcoin::secp256k1::Signature" => Some(".is_null()"),
729                         _ => None
730                 }
731         }
732
733         // ****************************
734         // *** Container Processing ***
735         // ****************************
736
737         /// Returns the module path in the generated mapping crate to the containers which we generate
738         /// when writing to CrateTypes::template_file.
739         pub fn generated_container_path() -> &'static str {
740                 "crate::c_types::derived"
741         }
742         /// Returns the module path in the generated mapping crate to the container templates, which
743         /// are then concretized and put in the generated container path/template_file.
744         fn container_templ_path() -> &'static str {
745                 "crate::c_types"
746         }
747
748         /// Returns true if this is a "transparent" container, ie an Option or a container which does
749         /// not require a generated continer class.
750         fn is_transparent_container(&self, full_path: &str, _is_ref: bool) -> bool {
751                 full_path == "Option"
752         }
753         /// Returns true if this is a known, supported, non-transparent container.
754         fn is_known_container(&self, full_path: &str, is_ref: bool) -> bool {
755                 (full_path == "Result" && !is_ref) || (full_path == "Vec" && !is_ref) || full_path.ends_with("Tuple")
756         }
757         fn to_c_conversion_container_new_var<'b>(&self, generics: Option<&GenericTypes>, full_path: &str, is_ref: bool, single_contained: Option<&syn::Type>, var_name: &syn::Ident, var_access: &str)
758                         // Returns prefix + Vec<(prefix, var-name-to-inline-convert)> + suffix
759                         // expecting one element in the vec per generic type, each of which is inline-converted
760                         -> Option<(&'b str, Vec<(String, String)>, &'b str)> {
761                 match full_path {
762                         "Result" if !is_ref => {
763                                 Some(("match ",
764                                                 vec![(" { Ok(mut o) => crate::c_types::CResultTempl::ok(".to_string(), "o".to_string()),
765                                                         (").into(), Err(mut e) => crate::c_types::CResultTempl::err(".to_string(), "e".to_string())],
766                                                 ").into() }"))
767                         },
768                         "Vec" if !is_ref => {
769                                 Some(("Vec::new(); for item in ", vec![(format!(".drain(..) {{ local_{}.push(", var_name), "item".to_string())], "); }"))
770                         },
771                         "Slice" => {
772                                 Some(("Vec::new(); for item in ", vec![(format!(".iter() {{ local_{}.push(", var_name), "**item".to_string())], "); }"))
773                         },
774                         "Option" => {
775                                 if let Some(syn::Type::Path(p)) = single_contained {
776                                         if self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics)) {
777                                                 if is_ref {
778                                                         return Some(("if ", vec![
779                                                                 (".is_none() { std::ptr::null() } else { ".to_owned(), format!("({}.as_ref().unwrap())", var_access))
780                                                                 ], " }"));
781                                                 } else {
782                                                         return Some(("if ", vec![
783                                                                 (".is_none() { std::ptr::null_mut() } else { ".to_owned(), format!("({}.unwrap())", var_access))
784                                                                 ], " }"));
785                                                 }
786                                         }
787                                 }
788                                 if let Some(t) = single_contained {
789                                         let mut v = Vec::new();
790                                         self.write_empty_rust_val(generics, &mut v, t);
791                                         let s = String::from_utf8(v).unwrap();
792                                         return Some(("if ", vec![
793                                                 (format!(".is_none() {{ {} }} else {{ ", s), format!("({}.unwrap())", var_access))
794                                                 ], " }"));
795                                 } else { unreachable!(); }
796                         },
797                         _ => None,
798                 }
799         }
800
801         /// only_contained_has_inner implies that there is only one contained element in the container
802         /// and it has an inner field (ie is an "opaque" type we've defined).
803         fn from_c_conversion_container_new_var<'b>(&self, generics: Option<&GenericTypes>, full_path: &str, is_ref: bool, single_contained: Option<&syn::Type>, var_name: &syn::Ident, var_access: &str)
804                         // Returns prefix + Vec<(prefix, var-name-to-inline-convert)> + suffix
805                         // expecting one element in the vec per generic type, each of which is inline-converted
806                         -> Option<(&'b str, Vec<(String, String)>, &'b str)> {
807                 match full_path {
808                         "Result" if !is_ref => {
809                                 Some(("match ",
810                                                 vec![(".result_ok { true => Ok(".to_string(), format!("(*unsafe {{ Box::from_raw(<*mut _>::take_ptr(&mut {}.contents.result)) }})", var_name)),
811                                                      ("), false => Err(".to_string(), format!("(*unsafe {{ Box::from_raw(<*mut _>::take_ptr(&mut {}.contents.err)) }})", var_name))],
812                                                 ")}"))
813                         },
814                         "Vec"|"Slice" if !is_ref => {
815                                 Some(("Vec::new(); for mut item in ", vec![(format!(".into_rust().drain(..) {{ local_{}.push(", var_name), "item".to_string())], "); }"))
816                         },
817                         "Slice" if is_ref => {
818                                 Some(("Vec::new(); for mut item in ", vec![(format!(".as_slice().iter() {{ local_{}.push(", var_name), "item".to_string())], "); }"))
819                         },
820                         "Option" => {
821                                 if let Some(syn::Type::Path(p)) = single_contained {
822                                         if self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics)) {
823                                                 if is_ref {
824                                                         return Some(("if ", vec![(".inner.is_null() { None } else { Some((*".to_string(), format!("{}", var_name))], ").clone()) }"))
825                                                 } else {
826                                                         return Some(("if ", vec![(".inner.is_null() { None } else { Some(".to_string(), format!("{}", var_name))], ") }"));
827                                                 }
828                                         }
829                                 }
830
831                                 if let Some(t) = single_contained {
832                                         let mut v = Vec::new();
833                                         let ret_ref = self.write_empty_rust_val_check_suffix(generics, &mut v, t);
834                                         let s = String::from_utf8(v).unwrap();
835                                         match ret_ref {
836                                                 EmptyValExpectedTy::ReferenceAsPointer =>
837                                                         return Some(("if ", vec![
838                                                                 (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ &mut *{} }}", var_access))
839                                                         ], ") }")),
840                                                 EmptyValExpectedTy::OwnedPointer =>
841                                                         return Some(("if ", vec![
842                                                                 (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ *Box::from_raw({}) }}", var_access))
843                                                         ], ") }")),
844                                                 EmptyValExpectedTy::NonPointer =>
845                                                         return Some(("if ", vec![
846                                                                 (format!("{} {{ None }} else {{ Some(", s), format!("{}", var_access))
847                                                         ], ") }")),
848                                         }
849                                 } else { unreachable!(); }
850                         },
851                         _ => None,
852                 }
853         }
854
855         // *************************************************
856         // *** Type definition during main.rs processing ***
857         // *************************************************
858
859         fn process_use_intern<W: std::io::Write>(&mut self, w: &mut W, u: &syn::UseTree, partial_path: &str) {
860                 match u {
861                         syn::UseTree::Path(p) => {
862                                 let new_path = format!("{}::{}", partial_path, p.ident);
863                                 self.process_use_intern(w, &p.tree, &new_path);
864                         },
865                         syn::UseTree::Name(n) => {
866                                 let full_path = format!("{}::{}", partial_path, n.ident);
867                                 self.imports.insert(n.ident.clone(), full_path);
868                         },
869                         syn::UseTree::Group(g) => {
870                                 for i in g.items.iter() {
871                                         self.process_use_intern(w, i, partial_path);
872                                 }
873                         },
874                         syn::UseTree::Rename(r) => {
875                                 let full_path = format!("{}::{}", partial_path, r.ident);
876                                 self.imports.insert(r.rename.clone(), full_path);
877                         },
878                         syn::UseTree::Glob(_) => {
879                                 eprintln!("Ignoring * use for {} - this may result in resolution failures", partial_path);
880                         },
881                 }
882         }
883         pub fn process_use<W: std::io::Write>(&mut self, w: &mut W, u: &syn::ItemUse) {
884                 if let syn::Visibility::Public(_) = u.vis {
885                         // We actually only use these for #[cfg(fuzztarget)]
886                         eprintln!("Ignoring pub(use) tree!");
887                         return;
888                 }
889                 if u.leading_colon.is_some() { eprintln!("Ignoring leading-colon use!"); return; }
890                 match &u.tree {
891                         syn::UseTree::Path(p) => {
892                                 let new_path = format!("{}", p.ident);
893                                 self.process_use_intern(w, &p.tree, &new_path);
894                         },
895                         syn::UseTree::Name(n) => {
896                                 let full_path = format!("{}", n.ident);
897                                 self.imports.insert(n.ident.clone(), full_path);
898                         },
899                         _ => unimplemented!(),
900                 }
901         }
902
903         pub fn mirrored_enum_declared(&mut self, ident: &syn::Ident) {
904                 self.declared.insert(ident.clone(), DeclType::MirroredEnum);
905         }
906         pub fn enum_ignored(&mut self, ident: &'c syn::Ident) {
907                 self.declared.insert(ident.clone(), DeclType::EnumIgnored);
908         }
909         pub fn struct_imported(&mut self, ident: &'c syn::Ident) {
910                 self.declared.insert(ident.clone(), DeclType::StructImported);
911         }
912         pub fn struct_ignored(&mut self, ident: &syn::Ident) {
913                 eprintln!("Not importing {}", ident);
914                 self.declared.insert(ident.clone(), DeclType::StructIgnored);
915         }
916         pub fn trait_declared(&mut self, ident: &syn::Ident, t: &'c syn::ItemTrait) {
917                 self.declared.insert(ident.clone(), DeclType::Trait(t));
918         }
919         pub fn get_declared_type(&'a self, ident: &syn::Ident) -> Option<&'a DeclType<'c>> {
920                 self.declared.get(ident)
921         }
922         /// Returns true if the object at the given path is mapped as X { inner: *mut origX, .. }.
923         pub fn c_type_has_inner_from_path(&self, full_path: &str) -> bool{
924                 self.crate_types.opaques.get(full_path).is_some()
925         }
926
927         pub fn maybe_resolve_ident(&self, id: &syn::Ident) -> Option<String> {
928                 if let Some(imp) = self.imports.get(id) {
929                         Some(imp.clone())
930                 } else if self.declared.get(id).is_some() {
931                         Some(self.module_path.to_string() + "::" + &format!("{}", id))
932                 } else { None }
933         }
934
935         pub fn maybe_resolve_non_ignored_ident(&self, id: &syn::Ident) -> Option<String> {
936                 if let Some(imp) = self.imports.get(id) {
937                         Some(imp.clone())
938                 } else if let Some(decl_type) = self.declared.get(id) {
939                         match decl_type {
940                                 DeclType::StructIgnored => None,
941                                 _ => Some(self.module_path.to_string() + "::" + &format!("{}", id)),
942                         }
943                 } else { None }
944         }
945
946         pub fn maybe_resolve_path(&self, p_arg: &syn::Path, generics: Option<&GenericTypes>) -> Option<String> {
947                 let p = if let Some(gen_types) = generics {
948                         if let Some((_, synpath)) = gen_types.maybe_resolve_path(p_arg) {
949                                 synpath
950                         } else { p_arg }
951                 } else { p_arg };
952
953                 if p.leading_colon.is_some() {
954                         Some(p.segments.iter().enumerate().map(|(idx, seg)| {
955                                 format!("{}{}", if idx == 0 { "" } else { "::" }, seg.ident)
956                         }).collect())
957                 } else if let Some(id) = p.get_ident() {
958                         self.maybe_resolve_ident(id)
959                 } else {
960                         if p.segments.len() == 1 {
961                                 let seg = p.segments.iter().next().unwrap();
962                                 return self.maybe_resolve_ident(&seg.ident);
963                         }
964                         let mut seg_iter = p.segments.iter();
965                         let first_seg = seg_iter.next().unwrap();
966                         let remaining: String = seg_iter.map(|seg| {
967                                 format!("::{}", seg.ident)
968                         }).collect();
969                         if let Some(imp) = self.imports.get(&first_seg.ident) {
970                                 if remaining != "" {
971                                         Some(imp.clone() + &remaining)
972                                 } else {
973                                         Some(imp.clone())
974                                 }
975                         } else { None }
976                 }
977         }
978         pub fn resolve_path(&self, p: &syn::Path, generics: Option<&GenericTypes>) -> String {
979                 self.maybe_resolve_path(p, generics).unwrap()
980         }
981
982         // ***********************************
983         // *** Original Rust Type Printing ***
984         // ***********************************
985
986         fn in_rust_prelude(resolved_path: &str) -> bool {
987                 match resolved_path {
988                         "Vec" => true,
989                         "Result" => true,
990                         "Option" => true,
991                         _ => false,
992                 }
993         }
994
995         fn write_rust_path<W: std::io::Write>(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, path: &syn::Path) {
996                 if let Some(resolved) = self.maybe_resolve_path(&path, generics_resolver) {
997                         if self.is_primitive(&resolved) {
998                                 write!(w, "{}", path.get_ident().unwrap()).unwrap();
999                         } else {
1000                                 // TODO: We should have a generic "is from a dependency" check here instead of
1001                                 // checking for "bitcoin" explicitly.
1002                                 if resolved.starts_with("bitcoin::") || Self::in_rust_prelude(&resolved) {
1003                                         write!(w, "{}", resolved).unwrap();
1004                                 // If we're printing a generic argument, it needs to reference the crate, otherwise
1005                                 // the original crate:
1006                                 } else if self.maybe_resolve_path(&path, None).as_ref() == Some(&resolved) {
1007                                         write!(w, "{}::{}", self.orig_crate, resolved).unwrap();
1008                                 } else {
1009                                         write!(w, "crate::{}", resolved).unwrap();
1010                                 }
1011                         }
1012                         if let syn::PathArguments::AngleBracketed(args) = &path.segments.iter().last().unwrap().arguments {
1013                                 self.write_rust_generic_arg(w, generics_resolver, args.args.iter());
1014                         }
1015                 } else {
1016                         if path.leading_colon.is_some() {
1017                                 write!(w, "::").unwrap();
1018                         }
1019                         for (idx, seg) in path.segments.iter().enumerate() {
1020                                 if idx != 0 { write!(w, "::").unwrap(); }
1021                                 write!(w, "{}", seg.ident).unwrap();
1022                                 if let syn::PathArguments::AngleBracketed(args) = &seg.arguments {
1023                                         self.write_rust_generic_arg(w, generics_resolver, args.args.iter());
1024                                 }
1025                         }
1026                 }
1027         }
1028         pub fn write_rust_generic_param<'b, W: std::io::Write>(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, generics: impl Iterator<Item=&'b syn::GenericParam>) {
1029                 let mut had_params = false;
1030                 for (idx, arg) in generics.enumerate() {
1031                         if idx != 0 { write!(w, ", ").unwrap(); } else { write!(w, "<").unwrap(); }
1032                         had_params = true;
1033                         match arg {
1034                                 syn::GenericParam::Lifetime(lt) => write!(w, "'{}", lt.lifetime.ident).unwrap(),
1035                                 syn::GenericParam::Type(t) => {
1036                                         write!(w, "{}", t.ident).unwrap();
1037                                         if t.colon_token.is_some() { write!(w, ":").unwrap(); }
1038                                         for (idx, bound) in t.bounds.iter().enumerate() {
1039                                                 if idx != 0 { write!(w, " + ").unwrap(); }
1040                                                 match bound {
1041                                                         syn::TypeParamBound::Trait(tb) => {
1042                                                                 if tb.paren_token.is_some() || tb.lifetimes.is_some() { unimplemented!(); }
1043                                                                 self.write_rust_path(w, generics_resolver, &tb.path);
1044                                                         },
1045                                                         _ => unimplemented!(),
1046                                                 }
1047                                         }
1048                                         if t.eq_token.is_some() || t.default.is_some() { unimplemented!(); }
1049                                 },
1050                                 _ => unimplemented!(),
1051                         }
1052                 }
1053                 if had_params { write!(w, ">").unwrap(); }
1054         }
1055
1056         pub fn write_rust_generic_arg<'b, W: std::io::Write>(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, generics: impl Iterator<Item=&'b syn::GenericArgument>) {
1057                 write!(w, "<").unwrap();
1058                 for (idx, arg) in generics.enumerate() {
1059                         if idx != 0 { write!(w, ", ").unwrap(); }
1060                         match arg {
1061                                 syn::GenericArgument::Type(t) => self.write_rust_type(w, generics_resolver, t),
1062                                 _ => unimplemented!(),
1063                         }
1064                 }
1065                 write!(w, ">").unwrap();
1066         }
1067         pub fn write_rust_type<W: std::io::Write>(&self, w: &mut W, generics: Option<&GenericTypes>, t: &syn::Type) {
1068                 match t {
1069                         syn::Type::Path(p) => {
1070                                 if p.qself.is_some() {
1071                                         unimplemented!();
1072                                 }
1073                                 self.write_rust_path(w, generics, &p.path);
1074                         },
1075                         syn::Type::Reference(r) => {
1076                                 write!(w, "&").unwrap();
1077                                 if let Some(lft) = &r.lifetime {
1078                                         write!(w, "'{} ", lft.ident).unwrap();
1079                                 }
1080                                 if r.mutability.is_some() {
1081                                         write!(w, "mut ").unwrap();
1082                                 }
1083                                 self.write_rust_type(w, generics, &*r.elem);
1084                         },
1085                         syn::Type::Array(a) => {
1086                                 write!(w, "[").unwrap();
1087                                 self.write_rust_type(w, generics, &a.elem);
1088                                 if let syn::Expr::Lit(l) = &a.len {
1089                                         if let syn::Lit::Int(i) = &l.lit {
1090                                                 write!(w, "; {}]", i).unwrap();
1091                                         } else { unimplemented!(); }
1092                                 } else { unimplemented!(); }
1093                         }
1094                         syn::Type::Slice(s) => {
1095                                 write!(w, "[").unwrap();
1096                                 self.write_rust_type(w, generics, &s.elem);
1097                                 write!(w, "]").unwrap();
1098                         },
1099                         syn::Type::Tuple(s) => {
1100                                 write!(w, "(").unwrap();
1101                                 for (idx, t) in s.elems.iter().enumerate() {
1102                                         if idx != 0 { write!(w, ", ").unwrap(); }
1103                                         self.write_rust_type(w, generics, &t);
1104                                 }
1105                                 write!(w, ")").unwrap();
1106                         },
1107                         _ => unimplemented!(),
1108                 }
1109         }
1110
1111         /// Prints a constructor for something which is "uninitialized" (but obviously not actually
1112         /// unint'd memory).
1113         pub fn write_empty_rust_val<W: std::io::Write>(&self, generics: Option<&GenericTypes>, w: &mut W, t: &syn::Type) {
1114                 match t {
1115                         syn::Type::Path(p) => {
1116                                 let resolved = self.resolve_path(&p.path, generics);
1117                                 if self.crate_types.opaques.get(&resolved).is_some() {
1118                                         write!(w, "crate::{} {{ inner: std::ptr::null_mut(), is_owned: true }}", resolved).unwrap();
1119                                 } else {
1120                                         // Assume its a manually-mapped C type, where we can just define an null() fn
1121                                         write!(w, "{}::null()", self.c_type_from_path(&resolved, false, false).unwrap()).unwrap();
1122                                 }
1123                         },
1124                         syn::Type::Array(a) => {
1125                                 if let syn::Expr::Lit(l) = &a.len {
1126                                         if let syn::Lit::Int(i) = &l.lit {
1127                                                 if i.base10_digits().parse::<usize>().unwrap() < 32 {
1128                                                         // Blindly assume that if we're trying to create an empty value for an
1129                                                         // array < 32 entries that all-0s may be a valid state.
1130                                                         unimplemented!();
1131                                                 }
1132                                                 let arrty = format!("[u8; {}]", i.base10_digits());
1133                                                 write!(w, "{}", self.to_c_conversion_inline_prefix_from_path(&arrty, false, false).unwrap()).unwrap();
1134                                                 write!(w, "[0; {}]", i.base10_digits()).unwrap();
1135                                                 write!(w, "{}", self.to_c_conversion_inline_suffix_from_path(&arrty, false, false).unwrap()).unwrap();
1136                                         } else { unimplemented!(); }
1137                                 } else { unimplemented!(); }
1138                         }
1139                         _ => unimplemented!(),
1140                 }
1141         }
1142
1143         /// Prints a suffix to determine if a variable is empty (ie was set by write_empty_rust_val).
1144         /// See EmptyValExpectedTy for information on return types.
1145         fn write_empty_rust_val_check_suffix<W: std::io::Write>(&self, generics: Option<&GenericTypes>, w: &mut W, t: &syn::Type) -> EmptyValExpectedTy {
1146                 match t {
1147                         syn::Type::Path(p) => {
1148                                 let resolved = self.resolve_path(&p.path, generics);
1149                                 if self.crate_types.opaques.get(&resolved).is_some() {
1150                                         write!(w, ".inner.is_null()").unwrap();
1151                                         EmptyValExpectedTy::NonPointer
1152                                 } else {
1153                                         if let Some(suffix) = self.empty_val_check_suffix_from_path(&resolved) {
1154                                                 write!(w, "{}", suffix).unwrap();
1155                                                 // We may eventually need to allow empty_val_check_suffix_from_path to specify if we need a deref or not
1156                                                 EmptyValExpectedTy::NonPointer
1157                                         } else {
1158                                                 write!(w, " == std::ptr::null_mut()").unwrap();
1159                                                 EmptyValExpectedTy::OwnedPointer
1160                                         }
1161                                 }
1162                         },
1163                         syn::Type::Array(a) => {
1164                                 if let syn::Expr::Lit(l) = &a.len {
1165                                         if let syn::Lit::Int(i) = &l.lit {
1166                                                 write!(w, " == [0; {}]", i.base10_digits()).unwrap();
1167                                                 EmptyValExpectedTy::NonPointer
1168                                         } else { unimplemented!(); }
1169                                 } else { unimplemented!(); }
1170                         },
1171                         syn::Type::Slice(_) => {
1172                                 // Option<[]> always implies that we want to treat len() == 0 differently from
1173                                 // None, so we always map an Option<[]> into a pointer.
1174                                 write!(w, " == std::ptr::null_mut()").unwrap();
1175                                 EmptyValExpectedTy::ReferenceAsPointer
1176                         },
1177                         _ => unimplemented!(),
1178                 }
1179         }
1180
1181         /// Prints a suffix to determine if a variable is empty (ie was set by write_empty_rust_val).
1182         pub fn write_empty_rust_val_check<W: std::io::Write>(&self, generics: Option<&GenericTypes>, w: &mut W, t: &syn::Type, var_access: &str) {
1183                 match t {
1184                         syn::Type::Path(_) => {
1185                                 write!(w, "{}", var_access).unwrap();
1186                                 self.write_empty_rust_val_check_suffix(generics, w, t);
1187                         },
1188                         syn::Type::Array(a) => {
1189                                 if let syn::Expr::Lit(l) = &a.len {
1190                                         if let syn::Lit::Int(i) = &l.lit {
1191                                                 let arrty = format!("[u8; {}]", i.base10_digits());
1192                                                 // We don't (yet) support a new-var conversion here.
1193                                                 assert!(self.from_c_conversion_new_var_from_path(&arrty, false).is_none());
1194                                                 write!(w, "{}{}{}",
1195                                                         self.from_c_conversion_prefix_from_path(&arrty, false).unwrap(),
1196                                                         var_access,
1197                                                         self.from_c_conversion_suffix_from_path(&arrty, false).unwrap()).unwrap();
1198                                                 self.write_empty_rust_val_check_suffix(generics, w, t);
1199                                         } else { unimplemented!(); }
1200                                 } else { unimplemented!(); }
1201                         }
1202                         _ => unimplemented!(),
1203                 }
1204         }
1205
1206         // ********************************
1207         // *** Type conversion printing ***
1208         // ********************************
1209
1210         /// Returns true we if can just skip passing this to C entirely
1211         pub fn skip_arg(&self, t: &syn::Type, generics: Option<&GenericTypes>) -> bool {
1212                 match t {
1213                         syn::Type::Path(p) => {
1214                                 if p.qself.is_some() { unimplemented!(); }
1215                                 if let Some(full_path) = self.maybe_resolve_path(&p.path, generics) {
1216                                         self.skip_path(&full_path)
1217                                 } else { false }
1218                         },
1219                         syn::Type::Reference(r) => {
1220                                 self.skip_arg(&*r.elem, generics)
1221                         },
1222                         _ => false,
1223                 }
1224         }
1225         pub fn no_arg_to_rust<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>) {
1226                 match t {
1227                         syn::Type::Path(p) => {
1228                                 if p.qself.is_some() { unimplemented!(); }
1229                                 if let Some(full_path) = self.maybe_resolve_path(&p.path, generics) {
1230                                         write!(w, "{}", self.no_arg_path_to_rust(&full_path)).unwrap();
1231                                 }
1232                         },
1233                         syn::Type::Reference(r) => {
1234                                 self.no_arg_to_rust(w, &*r.elem, generics);
1235                         },
1236                         _ => {},
1237                 }
1238         }
1239
1240         fn write_conversion_inline_intern<W: std::io::Write,
1241                         LP: Fn(&str, bool, bool) -> Option<String>, DL: Fn(&mut W, &DeclType, &str, bool, bool), SC: Fn(bool) -> &'static str>
1242                         (&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, is_ref: bool, is_mut: bool, ptr_for_ref: bool,
1243                          tupleconv: &str, prefix: bool, sliceconv: SC, path_lookup: LP, decl_lookup: DL) {
1244                 match t {
1245                         syn::Type::Reference(r) => {
1246                                 self.write_conversion_inline_intern(w, &*r.elem, generics, true, r.mutability.is_some(),
1247                                         ptr_for_ref, tupleconv, prefix, sliceconv, path_lookup, decl_lookup);
1248                         },
1249                         syn::Type::Path(p) => {
1250                                 if p.qself.is_some() {
1251                                         unimplemented!();
1252                                 }
1253
1254                                 let resolved_path = self.resolve_path(&p.path, generics);
1255                                 if let Some(aliased_type) = self.crate_types.type_aliases.get(&resolved_path) {
1256                                         return self.write_conversion_inline_intern(w, aliased_type, None, is_ref, is_mut, ptr_for_ref, tupleconv, prefix, sliceconv, path_lookup, decl_lookup);
1257                                 } else if let Some(c_type) = path_lookup(&resolved_path, is_ref, ptr_for_ref) {
1258                                         write!(w, "{}", c_type).unwrap();
1259                                 } else if self.crate_types.opaques.get(&resolved_path).is_some() {
1260                                         decl_lookup(w, &DeclType::StructImported, &resolved_path, is_ref, is_mut);
1261                                 } else if self.crate_types.mirrored_enums.get(&resolved_path).is_some() {
1262                                         decl_lookup(w, &DeclType::MirroredEnum, &resolved_path, is_ref, is_mut);
1263                                 } else if let Some(t) = self.crate_types.traits.get(&resolved_path) {
1264                                         decl_lookup(w, &DeclType::Trait(t), &resolved_path, is_ref, is_mut);
1265                                 } else if let Some(ident) = single_ident_generic_path_to_ident(&p.path) {
1266                                         if let Some(_) = self.imports.get(ident) {
1267                                                 // crate_types lookup has to have succeeded:
1268                                                 panic!("Failed to print inline conversion for {}", ident);
1269                                         } else if let Some(decl_type) = self.declared.get(ident) {
1270                                                 decl_lookup(w, decl_type, &self.maybe_resolve_ident(ident).unwrap(), is_ref, is_mut);
1271                                         } else { unimplemented!(); }
1272                                 } else { unimplemented!(); }
1273                         },
1274                         syn::Type::Array(a) => {
1275                                 // We assume all arrays contain only [int_literal; X]s.
1276                                 // This may result in some outputs not compiling.
1277                                 if let syn::Expr::Lit(l) = &a.len {
1278                                         if let syn::Lit::Int(i) = &l.lit {
1279                                                 write!(w, "{}", path_lookup(&format!("[u8; {}]", i.base10_digits()), is_ref, ptr_for_ref).unwrap()).unwrap();
1280                                         } else { unimplemented!(); }
1281                                 } else { unimplemented!(); }
1282                         },
1283                         syn::Type::Slice(s) => {
1284                                 // We assume all slices contain only literals or references.
1285                                 // This may result in some outputs not compiling.
1286                                 if let syn::Type::Path(p) = &*s.elem {
1287                                         let resolved = self.resolve_path(&p.path, generics);
1288                                         assert!(self.is_primitive(&resolved));
1289                                         write!(w, "{}", path_lookup("[u8]", is_ref, ptr_for_ref).unwrap()).unwrap();
1290                                 } else if let syn::Type::Reference(r) = &*s.elem {
1291                                         if let syn::Type::Path(p) = &*r.elem {
1292                                                 write!(w, "{}", sliceconv(self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics)))).unwrap();
1293                                         } else { unimplemented!(); }
1294                                 } else if let syn::Type::Tuple(t) = &*s.elem {
1295                                         assert!(!t.elems.is_empty());
1296                                         if prefix {
1297                                                 write!(w, "&local_").unwrap();
1298                                         } else {
1299                                                 let mut needs_map = false;
1300                                                 for e in t.elems.iter() {
1301                                                         if let syn::Type::Reference(_) = e {
1302                                                                 needs_map = true;
1303                                                         }
1304                                                 }
1305                                                 if needs_map {
1306                                                         write!(w, ".iter().map(|(").unwrap();
1307                                                         for i in 0..t.elems.len() {
1308                                                                 write!(w, "{}{}", if i != 0 { ", " } else { "" }, ('a' as u8 + i as u8) as char).unwrap();
1309                                                         }
1310                                                         write!(w, ")| (").unwrap();
1311                                                         for (idx, e) in t.elems.iter().enumerate() {
1312                                                                 if let syn::Type::Reference(_) = e {
1313                                                                         write!(w, "{}{}", if idx != 0 { ", " } else { "" }, (idx as u8 + 'a' as u8) as char).unwrap();
1314                                                                 } else if let syn::Type::Path(_) = e {
1315                                                                         write!(w, "{}*{}", if idx != 0 { ", " } else { "" }, (idx as u8 + 'a' as u8) as char).unwrap();
1316                                                                 } else { unimplemented!(); }
1317                                                         }
1318                                                         write!(w, ")).collect::<Vec<_>>()[..]").unwrap();
1319                                                 }
1320                                         }
1321                                 } else { unimplemented!(); }
1322                         },
1323                         syn::Type::Tuple(t) => {
1324                                 if t.elems.is_empty() {
1325                                         // cbindgen has poor support for (), see, eg https://github.com/eqrion/cbindgen/issues/527
1326                                         // so work around it by just pretending its a 0u8
1327                                         write!(w, "{}", tupleconv).unwrap();
1328                                 } else {
1329                                         if prefix { write!(w, "local_").unwrap(); }
1330                                 }
1331                         },
1332                         _ => unimplemented!(),
1333                 }
1334         }
1335
1336         fn write_to_c_conversion_inline_prefix_inner<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, is_ref: bool, ptr_for_ref: bool, from_ptr: bool) {
1337                 self.write_conversion_inline_intern(w, t, generics, is_ref, false, ptr_for_ref, "0u8 /*", true, |_| "local_",
1338                                 |a, b, c| self.to_c_conversion_inline_prefix_from_path(a, b, c),
1339                                 |w, decl_type, decl_path, is_ref, _is_mut| {
1340                                         match decl_type {
1341                                                 DeclType::MirroredEnum if is_ref && ptr_for_ref => write!(w, "crate::{}::from_native(&", decl_path).unwrap(),
1342                                                 DeclType::MirroredEnum if is_ref => write!(w, "&crate::{}::from_native(&", decl_path).unwrap(),
1343                                                 DeclType::MirroredEnum => write!(w, "crate::{}::native_into(", decl_path).unwrap(),
1344                                                 DeclType::EnumIgnored|DeclType::StructImported if is_ref && ptr_for_ref && from_ptr =>
1345                                                         write!(w, "crate::{} {{ inner: unsafe {{ (", decl_path).unwrap(),
1346                                                 DeclType::EnumIgnored|DeclType::StructImported if is_ref && ptr_for_ref =>
1347                                                         write!(w, "crate::{} {{ inner: unsafe {{ ( (&(", decl_path).unwrap(),
1348                                                 DeclType::EnumIgnored|DeclType::StructImported if is_ref =>
1349                                                         write!(w, "&crate::{} {{ inner: unsafe {{ (", decl_path).unwrap(),
1350                                                 DeclType::EnumIgnored|DeclType::StructImported if !is_ref && from_ptr =>
1351                                                         write!(w, "crate::{} {{ inner: ", decl_path).unwrap(),
1352                                                 DeclType::EnumIgnored|DeclType::StructImported if !is_ref =>
1353                                                         write!(w, "crate::{} {{ inner: Box::into_raw(Box::new(", decl_path).unwrap(),
1354                                                 DeclType::Trait(_) if is_ref => write!(w, "&").unwrap(),
1355                                                 DeclType::Trait(_) if !is_ref => {},
1356                                                 _ => panic!("{:?}", decl_path),
1357                                         }
1358                                 });
1359         }
1360         pub fn write_to_c_conversion_inline_prefix<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, ptr_for_ref: bool) {
1361                 self.write_to_c_conversion_inline_prefix_inner(w, t, generics, false, ptr_for_ref, false);
1362         }
1363         fn write_to_c_conversion_inline_suffix_inner<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, is_ref: bool, ptr_for_ref: bool, from_ptr: bool) {
1364                 self.write_conversion_inline_intern(w, t, generics, is_ref, false, ptr_for_ref, "*/", false, |_| ".into()",
1365                                 |a, b, c| self.to_c_conversion_inline_suffix_from_path(a, b, c),
1366                                 |w, decl_type, _full_path, is_ref, _is_mut| match decl_type {
1367                                         DeclType::MirroredEnum => write!(w, ")").unwrap(),
1368                                         DeclType::EnumIgnored|DeclType::StructImported if is_ref && ptr_for_ref && from_ptr =>
1369                                                 write!(w, " as *const _) as *mut _ }}, is_owned: false }}").unwrap(),
1370                                         DeclType::EnumIgnored|DeclType::StructImported if is_ref && ptr_for_ref =>
1371                                                 write!(w, ") as *const _) as *mut _) }}, is_owned: false }}").unwrap(),
1372                                         DeclType::EnumIgnored|DeclType::StructImported if is_ref =>
1373                                                 write!(w, " as *const _) as *mut _ }}, is_owned: false }}").unwrap(),
1374                                         DeclType::EnumIgnored|DeclType::StructImported if !is_ref && from_ptr =>
1375                                                 write!(w, ", is_owned: true }}").unwrap(),
1376                                         DeclType::EnumIgnored|DeclType::StructImported if !is_ref => write!(w, ")), is_owned: true }}").unwrap(),
1377                                         DeclType::Trait(_) if is_ref => {},
1378                                         DeclType::Trait(_) => {
1379                                                 // This is used when we're converting a concrete Rust type into a C trait
1380                                                 // for use when a Rust trait method returns an associated type.
1381                                                 // Because all of our C traits implement From<RustTypesImplementingTraits>
1382                                                 // we can just call .into() here and be done.
1383                                                 write!(w, ".into()").unwrap()
1384                                         },
1385                                         _ => unimplemented!(),
1386                                 });
1387         }
1388         pub fn write_to_c_conversion_inline_suffix<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, ptr_for_ref: bool) {
1389                 self.write_to_c_conversion_inline_suffix_inner(w, t, generics, false, ptr_for_ref, false);
1390         }
1391
1392         fn write_from_c_conversion_prefix_inner<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, is_ref: bool, ptr_for_ref: bool) {
1393                 self.write_conversion_inline_intern(w, t, generics, is_ref, false, false, "() /*", true, |_| "&local_",
1394                                 |a, b, _c| self.from_c_conversion_prefix_from_path(a, b),
1395                                 |w, decl_type, _full_path, is_ref, is_mut| match decl_type {
1396                                         DeclType::StructImported if is_ref && ptr_for_ref => write!(w, "unsafe {{ &*(*").unwrap(),
1397                                         DeclType::StructImported if is_mut && is_ref => write!(w, "unsafe {{ &mut *").unwrap(),
1398                                         DeclType::StructImported if is_ref => write!(w, "unsafe {{ &*").unwrap(),
1399                                         DeclType::StructImported if !is_ref => write!(w, "*unsafe {{ Box::from_raw(").unwrap(),
1400                                         DeclType::MirroredEnum if is_ref => write!(w, "&").unwrap(),
1401                                         DeclType::MirroredEnum => {},
1402                                         DeclType::Trait(_) => {},
1403                                         _ => unimplemented!(),
1404                                 });
1405         }
1406         pub fn write_from_c_conversion_prefix<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>) {
1407                 self.write_from_c_conversion_prefix_inner(w, t, generics, false, false);
1408         }
1409         fn write_from_c_conversion_suffix_inner<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, is_ref: bool, ptr_for_ref: bool) {
1410                 self.write_conversion_inline_intern(w, t, generics, is_ref, false, false, "*/", false,
1411                                 |has_inner| match has_inner {
1412                                         false => ".iter().collect::<Vec<_>>()[..]",
1413                                         true => "[..]",
1414                                 },
1415                                 |a, b, _c| self.from_c_conversion_suffix_from_path(a, b),
1416                                 |w, decl_type, _full_path, is_ref, _is_mut| match decl_type {
1417                                         DeclType::StructImported if is_ref && ptr_for_ref => write!(w, ").inner }}").unwrap(),
1418                                         DeclType::StructImported if is_ref => write!(w, ".inner }}").unwrap(),
1419                                         DeclType::StructImported if !is_ref => write!(w, ".take_inner()) }}").unwrap(),
1420                                         DeclType::MirroredEnum if is_ref => write!(w, ".to_native()").unwrap(),
1421                                         DeclType::MirroredEnum => write!(w, ".into_native()").unwrap(),
1422                                         DeclType::Trait(_) => {},
1423                                         _ => unimplemented!(),
1424                                 });
1425         }
1426         pub fn write_from_c_conversion_suffix<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>) {
1427                 self.write_from_c_conversion_suffix_inner(w, t, generics, false, false);
1428         }
1429         // Note that compared to the above conversion functions, the following two are generally
1430         // significantly undertested:
1431         pub fn write_from_c_conversion_to_ref_prefix<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>) {
1432                 self.write_conversion_inline_intern(w, t, generics, false, false, false, "() /*", true, |_| "&local_",
1433                                 |a, b, _c| {
1434                                         if let Some(conv) = self.from_c_conversion_prefix_from_path(a, b) {
1435                                                 Some(format!("&{}", conv))
1436                                         } else { None }
1437                                 },
1438                                 |w, decl_type, _full_path, is_ref, _is_mut| match decl_type {
1439                                         DeclType::StructImported if !is_ref => write!(w, "unsafe {{ &*").unwrap(),
1440                                         _ => unimplemented!(),
1441                                 });
1442         }
1443         pub fn write_from_c_conversion_to_ref_suffix<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>) {
1444                 self.write_conversion_inline_intern(w, t, generics, false, false, false, "*/", false,
1445                                 |has_inner| match has_inner {
1446                                         false => ".iter().collect::<Vec<_>>()[..]",
1447                                         true => "[..]",
1448                                 },
1449                                 |a, b, _c| self.from_c_conversion_suffix_from_path(a, b),
1450                                 |w, decl_type, _full_path, is_ref, _is_mut| match decl_type {
1451                                         DeclType::StructImported if !is_ref => write!(w, ".inner }}").unwrap(),
1452                                         _ => unimplemented!(),
1453                                 });
1454         }
1455
1456         fn write_conversion_new_var_intern<'b, W: std::io::Write,
1457                 LP: Fn(&str, bool) -> Option<(&str, &str)>,
1458                 LC: Fn(&str, bool, Option<&syn::Type>, &syn::Ident, &str) ->  Option<(&'b str, Vec<(String, String)>, &'b str)>,
1459                 VP: Fn(&mut W, &syn::Type, Option<&GenericTypes>, bool, bool, bool),
1460                 VS: Fn(&mut W, &syn::Type, Option<&GenericTypes>, bool, bool, bool)>
1461                         (&self, w: &mut W, ident: &syn::Ident, var: &str, t: &syn::Type, generics: Option<&GenericTypes>,
1462                          mut is_ref: bool, mut ptr_for_ref: bool, to_c: bool,
1463                          path_lookup: &LP, container_lookup: &LC, var_prefix: &VP, var_suffix: &VS) -> bool {
1464
1465                 macro_rules! convert_container {
1466                         ($container_type: expr, $args_len: expr, $args_iter: expr) => { {
1467                                 // For slices (and Options), we refuse to directly map them as is_ref when they
1468                                 // aren't opaque types containing an inner pointer. This is due to the fact that,
1469                                 // in both cases, the actual higher-level type is non-is_ref.
1470                                 let ty_has_inner = if self.is_transparent_container(&$container_type, is_ref) || $container_type == "Slice" {
1471                                         let ty = $args_iter().next().unwrap();
1472                                         if $container_type == "Slice" && to_c {
1473                                                 // "To C ptr_for_ref" means "return the regular object with is_owned
1474                                                 // set to false", which is totally what we want in a slice if we're about to
1475                                                 // set ty_has_inner.
1476                                                 ptr_for_ref = true;
1477                                         }
1478                                         if let syn::Type::Reference(t) = ty {
1479                                                 if let syn::Type::Path(p) = &*t.elem {
1480                                                         self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics))
1481                                                 } else { false }
1482                                         } else if let syn::Type::Path(p) = ty {
1483                                                 self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics))
1484                                         } else { false }
1485                                 } else { true };
1486
1487                                 // Options get a bunch of special handling, since in general we map Option<>al
1488                                 // types into the same C type as non-Option-wrapped types. This ends up being
1489                                 // pretty manual here and most of the below special-cases are for Options.
1490                                 let mut needs_ref_map = false;
1491                                 let mut only_contained_type = None;
1492                                 let mut only_contained_has_inner = false;
1493                                 let mut contains_slice = false;
1494                                 if $args_len == 1 && self.is_transparent_container(&$container_type, is_ref) {
1495                                         only_contained_has_inner = ty_has_inner;
1496                                         let arg = $args_iter().next().unwrap();
1497                                         if let syn::Type::Reference(t) = arg {
1498                                                 only_contained_type = Some(&*t.elem);
1499                                                 if let syn::Type::Path(_) = &*t.elem {
1500                                                         is_ref = true;
1501                                                 } else if let syn::Type::Slice(_) = &*t.elem {
1502                                                         contains_slice = true;
1503                                                 } else { return false; }
1504                                                 needs_ref_map = true;
1505                                         } else if let syn::Type::Path(_) = arg {
1506                                                 only_contained_type = Some(&arg);
1507                                         } else { unimplemented!(); }
1508                                 }
1509
1510                                 if let Some((prefix, conversions, suffix)) = container_lookup(&$container_type, is_ref && ty_has_inner, only_contained_type, ident, var) {
1511                                         assert_eq!(conversions.len(), $args_len);
1512                                         write!(w, "let mut local_{}{} = ", ident, if !to_c && needs_ref_map {"_base"} else { "" }).unwrap();
1513                                         if only_contained_has_inner && to_c {
1514                                                 var_prefix(w, $args_iter().next().unwrap(), generics, is_ref, ptr_for_ref, true);
1515                                         }
1516                                         write!(w, "{}{}", prefix, var).unwrap();
1517
1518                                         for ((pfx, var_name), (idx, ty)) in conversions.iter().zip($args_iter().enumerate()) {
1519                                                 let mut var = std::io::Cursor::new(Vec::new());
1520                                                 write!(&mut var, "{}", var_name).unwrap();
1521                                                 let var_access = String::from_utf8(var.into_inner()).unwrap();
1522
1523                                                 let conv_ty = if needs_ref_map { only_contained_type.as_ref().unwrap() } else { ty };
1524
1525                                                 write!(w, "{} {{ ", pfx).unwrap();
1526                                                 let new_var_name = format!("{}_{}", ident, idx);
1527                                                 let new_var = self.write_conversion_new_var_intern(w, &syn::Ident::new(&new_var_name, Span::call_site()),
1528                                                                 &var_access, conv_ty, generics, contains_slice || (is_ref && ty_has_inner), ptr_for_ref, to_c, path_lookup, container_lookup, var_prefix, var_suffix);
1529                                                 if new_var { write!(w, " ").unwrap(); }
1530                                                 if (!only_contained_has_inner || !to_c) && !contains_slice {
1531                                                         var_prefix(w, conv_ty, generics, is_ref && ty_has_inner, ptr_for_ref, false);
1532                                                 }
1533
1534                                                 if !is_ref && !needs_ref_map && to_c && only_contained_has_inner {
1535                                                         write!(w, "Box::into_raw(Box::new(").unwrap();
1536                                                 }
1537                                                 write!(w, "{}{}", if contains_slice { "local_" } else { "" }, if new_var { new_var_name } else { var_access }).unwrap();
1538                                                 if (!only_contained_has_inner || !to_c) && !contains_slice {
1539                                                         var_suffix(w, conv_ty, generics, is_ref && ty_has_inner, ptr_for_ref, false);
1540                                                 }
1541                                                 if !is_ref && !needs_ref_map && to_c && only_contained_has_inner {
1542                                                         write!(w, "))").unwrap();
1543                                                 }
1544                                                 write!(w, " }}").unwrap();
1545                                         }
1546                                         write!(w, "{}", suffix).unwrap();
1547                                         if only_contained_has_inner && to_c {
1548                                                 var_suffix(w, $args_iter().next().unwrap(), generics, is_ref, ptr_for_ref, true);
1549                                         }
1550                                         write!(w, ";").unwrap();
1551                                         if !to_c && needs_ref_map {
1552                                                 write!(w, " let mut local_{} = local_{}_base.as_ref()", ident, ident).unwrap();
1553                                                 if contains_slice {
1554                                                         write!(w, ".map(|a| &a[..])").unwrap();
1555                                                 }
1556                                                 write!(w, ";").unwrap();
1557                                         }
1558                                         return true;
1559                                 }
1560                         } }
1561                 }
1562
1563                 match t {
1564                         syn::Type::Reference(r) => {
1565                                 if let syn::Type::Slice(_) = &*r.elem {
1566                                         self.write_conversion_new_var_intern(w, ident, var, &*r.elem, generics, is_ref, ptr_for_ref, to_c, path_lookup, container_lookup, var_prefix, var_suffix)
1567                                 } else {
1568                                         self.write_conversion_new_var_intern(w, ident, var, &*r.elem, generics, true, ptr_for_ref, to_c, path_lookup, container_lookup, var_prefix, var_suffix)
1569                                 }
1570                         },
1571                         syn::Type::Path(p) => {
1572                                 if p.qself.is_some() {
1573                                         unimplemented!();
1574                                 }
1575                                 let resolved_path = self.resolve_path(&p.path, generics);
1576                                 if let Some(aliased_type) = self.crate_types.type_aliases.get(&resolved_path) {
1577                                         return self.write_conversion_new_var_intern(w, ident, var, aliased_type, None, is_ref, ptr_for_ref, to_c, path_lookup, container_lookup, var_prefix, var_suffix);
1578                                 }
1579                                 if self.is_known_container(&resolved_path, is_ref) || self.is_transparent_container(&resolved_path, is_ref) {
1580                                         if let syn::PathArguments::AngleBracketed(args) = &p.path.segments.iter().next().unwrap().arguments {
1581                                                 convert_container!(resolved_path, args.args.len(), || args.args.iter().map(|arg| {
1582                                                         if let syn::GenericArgument::Type(ty) = arg {
1583                                                                 ty
1584                                                         } else { unimplemented!(); }
1585                                                 }));
1586                                         } else { unimplemented!(); }
1587                                 }
1588                                 if self.is_primitive(&resolved_path) {
1589                                         false
1590                                 } else if let Some(ty_ident) = single_ident_generic_path_to_ident(&p.path) {
1591                                         if let Some((prefix, suffix)) = path_lookup(&resolved_path, is_ref) {
1592                                                 write!(w, "let mut local_{} = {}{}{};", ident, prefix, var, suffix).unwrap();
1593                                                 true
1594                                         } else if self.declared.get(ty_ident).is_some() {
1595                                                 false
1596                                         } else { false }
1597                                 } else { false }
1598                         },
1599                         syn::Type::Array(_) => {
1600                                 // We assume all arrays contain only primitive types.
1601                                 // This may result in some outputs not compiling.
1602                                 false
1603                         },
1604                         syn::Type::Slice(s) => {
1605                                 if let syn::Type::Path(p) = &*s.elem {
1606                                         let resolved = self.resolve_path(&p.path, generics);
1607                                         assert!(self.is_primitive(&resolved));
1608                                         let slice_path = format!("[{}]", resolved);
1609                                         if let Some((prefix, suffix)) = path_lookup(&slice_path, true) {
1610                                                 write!(w, "let mut local_{} = {}{}{};", ident, prefix, var, suffix).unwrap();
1611                                                 true
1612                                         } else { false }
1613                                 } else if let syn::Type::Reference(ty) = &*s.elem {
1614                                         let tyref = [&*ty.elem];
1615                                         is_ref = true;
1616                                         convert_container!("Slice", 1, || tyref.iter());
1617                                         unimplemented!("convert_container should return true as container_lookup should succeed for slices");
1618                                 } else if let syn::Type::Tuple(t) = &*s.elem {
1619                                         // When mapping into a temporary new var, we need to own all the underlying objects.
1620                                         // Thus, we drop any references inside the tuple and convert with non-reference types.
1621                                         let mut elems = syn::punctuated::Punctuated::new();
1622                                         for elem in t.elems.iter() {
1623                                                 if let syn::Type::Reference(r) = elem {
1624                                                         elems.push((*r.elem).clone());
1625                                                 } else {
1626                                                         elems.push(elem.clone());
1627                                                 }
1628                                         }
1629                                         let ty = [syn::Type::Tuple(syn::TypeTuple {
1630                                                 paren_token: t.paren_token, elems
1631                                         })];
1632                                         is_ref = false;
1633                                         ptr_for_ref = true;
1634                                         convert_container!("Slice", 1, || ty.iter());
1635                                         unimplemented!("convert_container should return true as container_lookup should succeed for slices");
1636                                 } else { unimplemented!() }
1637                         },
1638                         syn::Type::Tuple(t) => {
1639                                 if !t.elems.is_empty() {
1640                                         // We don't (yet) support tuple elements which cannot be converted inline
1641                                         write!(w, "let (").unwrap();
1642                                         for idx in 0..t.elems.len() {
1643                                                 if idx != 0 { write!(w, ", ").unwrap(); }
1644                                                 write!(w, "{} orig_{}_{}", if is_ref { "ref" } else { "mut" }, ident, idx).unwrap();
1645                                         }
1646                                         write!(w, ") = {}{}; ", var, if !to_c { ".to_rust()" } else { "" }).unwrap();
1647                                         // Like other template types, tuples are always mapped as their non-ref
1648                                         // versions for types which have different ref mappings. Thus, we convert to
1649                                         // non-ref versions and handle opaque types with inner pointers manually.
1650                                         for (idx, elem) in t.elems.iter().enumerate() {
1651                                                 if let syn::Type::Path(p) = elem {
1652                                                         let v_name = format!("orig_{}_{}", ident, idx);
1653                                                         let tuple_elem_ident = syn::Ident::new(&v_name, Span::call_site());
1654                                                         if self.write_conversion_new_var_intern(w, &tuple_elem_ident, &v_name, elem, generics,
1655                                                                         false, ptr_for_ref, to_c,
1656                                                                         path_lookup, container_lookup, var_prefix, var_suffix) {
1657                                                                 write!(w, " ").unwrap();
1658                                                                 // Opaque types with inner pointers shouldn't ever create new stack
1659                                                                 // variables, so we don't handle it and just assert that it doesn't
1660                                                                 // here.
1661                                                                 assert!(!self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics)));
1662                                                         }
1663                                                 }
1664                                         }
1665                                         write!(w, "let mut local_{} = (", ident).unwrap();
1666                                         for (idx, elem) in t.elems.iter().enumerate() {
1667                                                 let ty_has_inner = {
1668                                                                 if to_c {
1669                                                                         // "To C ptr_for_ref" means "return the regular object with
1670                                                                         // is_owned set to false", which is totally what we want
1671                                                                         // if we're about to set ty_has_inner.
1672                                                                         ptr_for_ref = true;
1673                                                                 }
1674                                                                 if let syn::Type::Reference(t) = elem {
1675                                                                         if let syn::Type::Path(p) = &*t.elem {
1676                                                                                 self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics))
1677                                                                         } else { false }
1678                                                                 } else if let syn::Type::Path(p) = elem {
1679                                                                         self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics))
1680                                                                 } else { false }
1681                                                         };
1682                                                 if idx != 0 { write!(w, ", ").unwrap(); }
1683                                                 var_prefix(w, elem, generics, is_ref && ty_has_inner, ptr_for_ref, false);
1684                                                 if is_ref && ty_has_inner {
1685                                                         // For ty_has_inner, the regular var_prefix mapping will take a
1686                                                         // reference, so deref once here to make sure we keep the original ref.
1687                                                         write!(w, "*").unwrap();
1688                                                 }
1689                                                 write!(w, "orig_{}_{}", ident, idx).unwrap();
1690                                                 if is_ref && !ty_has_inner {
1691                                                         // If we don't have an inner variable's reference to maintain, just
1692                                                         // hope the type is Clonable and use that.
1693                                                         write!(w, ".clone()").unwrap();
1694                                                 }
1695                                                 var_suffix(w, elem, generics, is_ref && ty_has_inner, ptr_for_ref, false);
1696                                         }
1697                                         write!(w, "){};", if to_c { ".into()" } else { "" }).unwrap();
1698                                         true
1699                                 } else { false }
1700                         },
1701                         _ => unimplemented!(),
1702                 }
1703         }
1704
1705         pub fn write_to_c_conversion_new_var_inner<W: std::io::Write>(&self, w: &mut W, ident: &syn::Ident, var_access: &str, t: &syn::Type, generics: Option<&GenericTypes>, ptr_for_ref: bool) -> bool {
1706                 self.write_conversion_new_var_intern(w, ident, var_access, t, generics, false, ptr_for_ref, true,
1707                         &|a, b| self.to_c_conversion_new_var_from_path(a, b),
1708                         &|a, b, c, d, e| self.to_c_conversion_container_new_var(generics, a, b, c, d, e),
1709                         // We force ptr_for_ref here since we can't generate a ref on one line and use it later
1710                         &|a, b, c, d, e, f| self.write_to_c_conversion_inline_prefix_inner(a, b, c, d, e, f),
1711                         &|a, b, c, d, e, f| self.write_to_c_conversion_inline_suffix_inner(a, b, c, d, e, f))
1712         }
1713         pub fn write_to_c_conversion_new_var<W: std::io::Write>(&self, w: &mut W, ident: &syn::Ident, t: &syn::Type, generics: Option<&GenericTypes>, ptr_for_ref: bool) -> bool {
1714                 self.write_to_c_conversion_new_var_inner(w, ident, &format!("{}", ident), t, generics, ptr_for_ref)
1715         }
1716         pub fn write_from_c_conversion_new_var<W: std::io::Write>(&self, w: &mut W, ident: &syn::Ident, t: &syn::Type, generics: Option<&GenericTypes>) -> bool {
1717                 self.write_conversion_new_var_intern(w, ident, &format!("{}", ident), t, generics, false, false, false,
1718                         &|a, b| self.from_c_conversion_new_var_from_path(a, b),
1719                         &|a, b, c, d, e| self.from_c_conversion_container_new_var(generics, a, b, c, d, e),
1720                         // We force ptr_for_ref here since we can't generate a ref on one line and use it later
1721                         &|a, b, c, d, e, _f| self.write_from_c_conversion_prefix_inner(a, b, c, d, e),
1722                         &|a, b, c, d, e, _f| self.write_from_c_conversion_suffix_inner(a, b, c, d, e))
1723         }
1724
1725         // ******************************************************
1726         // *** C Container Type Equivalent and alias Printing ***
1727         // ******************************************************
1728
1729         fn write_template_generics<'b, W: std::io::Write>(&mut self, w: &mut W, args: &mut dyn Iterator<Item=&'b syn::Type>, generics: Option<&GenericTypes>, is_ref: bool) -> bool {
1730                 assert!(!is_ref); // We don't currently support outer reference types
1731                 for (idx, t) in args.enumerate() {
1732                         if idx != 0 {
1733                                 write!(w, ", ").unwrap();
1734                         }
1735                         if let syn::Type::Reference(r_arg) = t {
1736                                 if !self.write_c_type_intern(w, &*r_arg.elem, generics, false, false, false) { return false; }
1737
1738                                 // While write_c_type_intern, above is correct, we don't want to blindly convert a
1739                                 // reference to something stupid, so check that the container is either opaque or a
1740                                 // predefined type (currently only Transaction).
1741                                 if let syn::Type::Path(p_arg) = &*r_arg.elem {
1742                                         let resolved = self.resolve_path(&p_arg.path, generics);
1743                                         assert!(self.crate_types.opaques.get(&resolved).is_some() ||
1744                                                         self.c_type_from_path(&resolved, true, true).is_some(), "Template generics should be opaque or have a predefined mapping");
1745                                 } else { unimplemented!(); }
1746                         } else {
1747                                 if !self.write_c_type_intern(w, t, generics, false, false, false) { return false; }
1748                         }
1749                 }
1750                 true
1751         }
1752         fn check_create_container(&mut self, mangled_container: String, container_type: &str, args: Vec<&syn::Type>, generics: Option<&GenericTypes>, is_ref: bool) -> bool {
1753                 if !self.crate_types.templates_defined.get(&mangled_container).is_some() {
1754                         let mut created_container: Vec<u8> = Vec::new();
1755
1756                         if container_type == "Result" {
1757                                 let mut a_ty: Vec<u8> = Vec::new();
1758                                 if let syn::Type::Tuple(tup) = args.iter().next().unwrap() {
1759                                         if tup.elems.is_empty() {
1760                                                 write!(&mut a_ty, "()").unwrap();
1761                                         } else {
1762                                                 if !self.write_template_generics(&mut a_ty, &mut args.iter().map(|t| *t).take(1), generics, is_ref) { return false; }
1763                                         }
1764                                 } else {
1765                                         if !self.write_template_generics(&mut a_ty, &mut args.iter().map(|t| *t).take(1), generics, is_ref) { return false; }
1766                                 }
1767
1768                                 let mut b_ty: Vec<u8> = Vec::new();
1769                                 if let syn::Type::Tuple(tup) = args.iter().skip(1).next().unwrap() {
1770                                         if tup.elems.is_empty() {
1771                                                 write!(&mut b_ty, "()").unwrap();
1772                                         } else {
1773                                                 if !self.write_template_generics(&mut b_ty, &mut args.iter().map(|t| *t).skip(1), generics, is_ref) { return false; }
1774                                         }
1775                                 } else {
1776                                         if !self.write_template_generics(&mut b_ty, &mut args.iter().map(|t| *t).skip(1), generics, is_ref) { return false; }
1777                                 }
1778
1779                                 let ok_str = String::from_utf8(a_ty).unwrap();
1780                                 let err_str = String::from_utf8(b_ty).unwrap();
1781                                 let is_clonable = self.is_clonable(&ok_str) && self.is_clonable(&err_str);
1782                                 write_result_block(&mut created_container, &mangled_container, &ok_str, &err_str, is_clonable);
1783                                 if is_clonable {
1784                                         self.crate_types.clonable_types.insert(Self::generated_container_path().to_owned() + "::" + &mangled_container);
1785                                 }
1786                         } else if container_type == "Vec" {
1787                                 let mut a_ty: Vec<u8> = Vec::new();
1788                                 if !self.write_template_generics(&mut a_ty, &mut args.iter().map(|t| *t), generics, is_ref) { return false; }
1789                                 let ty = String::from_utf8(a_ty).unwrap();
1790                                 let is_clonable = self.is_clonable(&ty);
1791                                 write_vec_block(&mut created_container, &mangled_container, &ty, is_clonable);
1792                                 if is_clonable {
1793                                         self.crate_types.clonable_types.insert(Self::generated_container_path().to_owned() + "::" + &mangled_container);
1794                                 }
1795                         } else if container_type.ends_with("Tuple") {
1796                                 let mut tuple_args = Vec::new();
1797                                 let mut is_clonable = true;
1798                                 for arg in args.iter() {
1799                                         let mut ty: Vec<u8> = Vec::new();
1800                                         if !self.write_template_generics(&mut ty, &mut [arg].iter().map(|t| **t), generics, is_ref) { return false; }
1801                                         let ty_str = String::from_utf8(ty).unwrap();
1802                                         if !self.is_clonable(&ty_str) {
1803                                                 is_clonable = false;
1804                                         }
1805                                         tuple_args.push(ty_str);
1806                                 }
1807                                 write_tuple_block(&mut created_container, &mangled_container, &tuple_args, is_clonable);
1808                                 if is_clonable {
1809                                         self.crate_types.clonable_types.insert(Self::generated_container_path().to_owned() + "::" + &mangled_container);
1810                                 }
1811                         } else {
1812                                 unreachable!();
1813                         }
1814                         self.crate_types.templates_defined.insert(mangled_container.clone(), true);
1815
1816                         self.crate_types.template_file.write(&created_container).unwrap();
1817                 }
1818                 true
1819         }
1820         fn path_to_generic_args(path: &syn::Path) -> Vec<&syn::Type> {
1821                 if let syn::PathArguments::AngleBracketed(args) = &path.segments.iter().next().unwrap().arguments {
1822                         args.args.iter().map(|gen| if let syn::GenericArgument::Type(t) = gen { t } else { unimplemented!() }).collect()
1823                 } else { unimplemented!(); }
1824         }
1825         fn write_c_mangled_container_path_intern<W: std::io::Write>
1826                         (&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 {
1827                 let mut mangled_type: Vec<u8> = Vec::new();
1828                 if !self.is_transparent_container(ident, is_ref) {
1829                         write!(w, "C{}_", ident).unwrap();
1830                         write!(mangled_type, "C{}_", ident).unwrap();
1831                 } else { assert_eq!(args.len(), 1); }
1832                 for arg in args.iter() {
1833                         macro_rules! write_path {
1834                                 ($p_arg: expr, $extra_write: expr) => {
1835                                         if let Some(subtype) = self.maybe_resolve_path(&$p_arg.path, generics) {
1836                                                 if self.is_transparent_container(ident, is_ref) {
1837                                                         // We dont (yet) support primitives or containers inside transparent
1838                                                         // containers, so check for that first:
1839                                                         if self.is_primitive(&subtype) { return false; }
1840                                                         if self.is_known_container(&subtype, is_ref) { return false; }
1841                                                         if !in_type {
1842                                                                 if self.c_type_has_inner_from_path(&subtype) {
1843                                                                         if !self.write_c_path_intern(w, &$p_arg.path, generics, is_ref, is_mut, ptr_for_ref) { return false; }
1844                                                                 } else {
1845                                                                         // Option<T> needs to be converted to a *mut T, ie mut ptr-for-ref
1846                                                                         if !self.write_c_path_intern(w, &$p_arg.path, generics, true, true, true) { return false; }
1847                                                                 }
1848                                                         } else {
1849                                                                 if $p_arg.path.segments.len() == 1 {
1850                                                                         write!(w, "{}", $p_arg.path.segments.iter().next().unwrap().ident).unwrap();
1851                                                                 } else {
1852                                                                         return false;
1853                                                                 }
1854                                                         }
1855                                                 } else if self.is_known_container(&subtype, is_ref) || self.is_transparent_container(&subtype, is_ref) {
1856                                                         if !self.write_c_mangled_container_path_intern(w, Self::path_to_generic_args(&$p_arg.path), generics,
1857                                                                         &subtype, is_ref, is_mut, ptr_for_ref, true) {
1858                                                                 return false;
1859                                                         }
1860                                                         self.write_c_mangled_container_path_intern(&mut mangled_type, Self::path_to_generic_args(&$p_arg.path),
1861                                                                 generics, &subtype, is_ref, is_mut, ptr_for_ref, true);
1862                                                         if let Some(w2) = $extra_write as Option<&mut Vec<u8>> {
1863                                                                 self.write_c_mangled_container_path_intern(w2, Self::path_to_generic_args(&$p_arg.path),
1864                                                                         generics, &subtype, is_ref, is_mut, ptr_for_ref, true);
1865                                                         }
1866                                                 } else {
1867                                                         let id = subtype.rsplitn(2, ':').next().unwrap(); // Get the "Base" name of the resolved type
1868                                                         write!(w, "{}", id).unwrap();
1869                                                         write!(mangled_type, "{}", id).unwrap();
1870                                                         if let Some(w2) = $extra_write as Option<&mut Vec<u8>> {
1871                                                                 write!(w2, "{}", id).unwrap();
1872                                                         }
1873                                                 }
1874                                         } else { return false; }
1875                                 }
1876                         }
1877                         if let syn::Type::Tuple(tuple) = arg {
1878                                 if tuple.elems.len() == 0 {
1879                                         write!(w, "None").unwrap();
1880                                         write!(mangled_type, "None").unwrap();
1881                                 } else {
1882                                         let mut mangled_tuple_type: Vec<u8> = Vec::new();
1883
1884                                         // Figure out what the mangled type should look like. To disambiguate
1885                                         // ((A, B), C) and (A, B, C) we prefix the generic args with a _ and suffix
1886                                         // them with a Z. Ideally we wouldn't use Z, but not many special chars are
1887                                         // available for use in type names.
1888                                         write!(w, "C{}Tuple_", tuple.elems.len()).unwrap();
1889                                         write!(mangled_type, "C{}Tuple_", tuple.elems.len()).unwrap();
1890                                         write!(mangled_tuple_type, "C{}Tuple_", tuple.elems.len()).unwrap();
1891                                         for elem in tuple.elems.iter() {
1892                                                 if let syn::Type::Path(p) = elem {
1893                                                         write_path!(p, Some(&mut mangled_tuple_type));
1894                                                 } else if let syn::Type::Reference(refelem) = elem {
1895                                                         if let syn::Type::Path(p) = &*refelem.elem {
1896                                                                 write_path!(p, Some(&mut mangled_tuple_type));
1897                                                         } else { return false; }
1898                                                 } else { return false; }
1899                                         }
1900                                         write!(w, "Z").unwrap();
1901                                         write!(mangled_type, "Z").unwrap();
1902                                         write!(mangled_tuple_type, "Z").unwrap();
1903                                         if !self.check_create_container(String::from_utf8(mangled_tuple_type).unwrap(),
1904                                                         &format!("{}Tuple", tuple.elems.len()), tuple.elems.iter().collect(), generics, is_ref) {
1905                                                 return false;
1906                                         }
1907                                 }
1908                         } else if let syn::Type::Path(p_arg) = arg {
1909                                 write_path!(p_arg, None);
1910                         } else if let syn::Type::Reference(refty) = arg {
1911                                 if let syn::Type::Path(p_arg) = &*refty.elem {
1912                                         write_path!(p_arg, None);
1913                                 } else if let syn::Type::Slice(_) = &*refty.elem {
1914                                         // write_c_type will actually do exactly what we want here, we just need to
1915                                         // make it a pointer so that its an option. Note that we cannot always convert
1916                                         // the Vec-as-slice (ie non-ref types) containers, so sometimes need to be able
1917                                         // to edit it, hence we use *mut here instead of *const.
1918                                         if args.len() != 1 { return false; }
1919                                         write!(w, "*mut ").unwrap();
1920                                         self.write_c_type(w, arg, None, true);
1921                                 } else { return false; }
1922                         } else if let syn::Type::Array(a) = arg {
1923                                 if let syn::Type::Path(p_arg) = &*a.elem {
1924                                         let resolved = self.resolve_path(&p_arg.path, generics);
1925                                         if !self.is_primitive(&resolved) { return false; }
1926                                         if let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(len), .. }) = &a.len {
1927                                                 if self.c_type_from_path(&format!("[{}; {}]", resolved, len.base10_digits()), is_ref, ptr_for_ref).is_none() { return false; }
1928                                                 write!(w, "_{}{}", resolved, len.base10_digits()).unwrap();
1929                                                 write!(mangled_type, "_{}{}", resolved, len.base10_digits()).unwrap();
1930                                         } else { return false; }
1931                                 } else { return false; }
1932                         } else { return false; }
1933                 }
1934                 if self.is_transparent_container(ident, is_ref) { return true; }
1935                 // Push the "end of type" Z
1936                 write!(w, "Z").unwrap();
1937                 write!(mangled_type, "Z").unwrap();
1938
1939                 // Make sure the type is actually defined:
1940                 self.check_create_container(String::from_utf8(mangled_type).unwrap(), ident, args, generics, is_ref)
1941         }
1942         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 {
1943                 if !self.is_transparent_container(ident, is_ref) {
1944                         write!(w, "{}::", Self::generated_container_path()).unwrap();
1945                 }
1946                 self.write_c_mangled_container_path_intern(w, args, generics, ident, is_ref, is_mut, ptr_for_ref, false)
1947         }
1948
1949         // **********************************
1950         // *** C Type Equivalent Printing ***
1951         // **********************************
1952
1953         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 {
1954                 let full_path = match self.maybe_resolve_path(&path, generics) {
1955                         Some(path) => path, None => return false };
1956                 if let Some(c_type) = self.c_type_from_path(&full_path, is_ref, ptr_for_ref) {
1957                         write!(w, "{}", c_type).unwrap();
1958                         true
1959                 } else if self.crate_types.traits.get(&full_path).is_some() {
1960                         if is_ref && ptr_for_ref {
1961                                 write!(w, "*{} crate::{}", if is_mut { "mut" } else { "const" }, full_path).unwrap();
1962                         } else if is_ref {
1963                                 write!(w, "&{}crate::{}", if is_mut { "mut " } else { "" }, full_path).unwrap();
1964                         } else {
1965                                 write!(w, "crate::{}", full_path).unwrap();
1966                         }
1967                         true
1968                 } else if self.crate_types.opaques.get(&full_path).is_some() || self.crate_types.mirrored_enums.get(&full_path).is_some() {
1969                         if is_ref && ptr_for_ref {
1970                                 // ptr_for_ref implies we're returning the object, which we can't really do for
1971                                 // opaque or mirrored types without box'ing them, which is quite a waste, so return
1972                                 // the actual object itself (for opaque types we'll set the pointer to the actual
1973                                 // type and note that its a reference).
1974                                 write!(w, "crate::{}", full_path).unwrap();
1975                         } else if is_ref {
1976                                 write!(w, "&{}crate::{}", if is_mut { "mut " } else { "" }, full_path).unwrap();
1977                         } else {
1978                                 write!(w, "crate::{}", full_path).unwrap();
1979                         }
1980                         true
1981                 } else {
1982                         false
1983                 }
1984         }
1985         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 {
1986                 match t {
1987                         syn::Type::Path(p) => {
1988                                 if p.qself.is_some() {
1989                                         return false;
1990                                 }
1991                                 if let Some(full_path) = self.maybe_resolve_path(&p.path, generics) {
1992                                         if self.is_known_container(&full_path, is_ref) || self.is_transparent_container(&full_path, is_ref) {
1993                                                 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);
1994                                         }
1995                                         if let Some(aliased_type) = self.crate_types.type_aliases.get(&full_path).cloned() {
1996                                                 return self.write_c_type_intern(w, &aliased_type, None, is_ref, is_mut, ptr_for_ref);
1997                                         }
1998                                 }
1999                                 self.write_c_path_intern(w, &p.path, generics, is_ref, is_mut, ptr_for_ref)
2000                         },
2001                         syn::Type::Reference(r) => {
2002                                 self.write_c_type_intern(w, &*r.elem, generics, true, r.mutability.is_some(), ptr_for_ref)
2003                         },
2004                         syn::Type::Array(a) => {
2005                                 if is_ref && is_mut {
2006                                         write!(w, "*mut [").unwrap();
2007                                         if !self.write_c_type_intern(w, &a.elem, generics, false, false, ptr_for_ref) { return false; }
2008                                 } else if is_ref {
2009                                         write!(w, "*const [").unwrap();
2010                                         if !self.write_c_type_intern(w, &a.elem, generics, false, false, ptr_for_ref) { return false; }
2011                                 } else {
2012                                         let mut typecheck = Vec::new();
2013                                         if !self.write_c_type_intern(&mut typecheck, &a.elem, generics, false, false, ptr_for_ref) { return false; }
2014                                         if typecheck[..] != ['u' as u8, '8' as u8] { return false; }
2015                                 }
2016                                 if let syn::Expr::Lit(l) = &a.len {
2017                                         if let syn::Lit::Int(i) = &l.lit {
2018                                                 if !is_ref {
2019                                                         if let Some(ty) = self.c_type_from_path(&format!("[u8; {}]", i.base10_digits()), false, ptr_for_ref) {
2020                                                                 write!(w, "{}", ty).unwrap();
2021                                                                 true
2022                                                         } else { false }
2023                                                 } else {
2024                                                         write!(w, "; {}]", i).unwrap();
2025                                                         true
2026                                                 }
2027                                         } else { false }
2028                                 } else { false }
2029                         }
2030                         syn::Type::Slice(s) => {
2031                                 if !is_ref || is_mut { return false; }
2032                                 if let syn::Type::Path(p) = &*s.elem {
2033                                         let resolved = self.resolve_path(&p.path, generics);
2034                                         if self.is_primitive(&resolved) {
2035                                                 write!(w, "{}::{}slice", Self::container_templ_path(), resolved).unwrap();
2036                                                 true
2037                                         } else { false }
2038                                 } else if let syn::Type::Reference(r) = &*s.elem {
2039                                         if let syn::Type::Path(p) = &*r.elem {
2040                                                 // Slices with "real types" inside are mapped as the equivalent non-ref Vec
2041                                                 let resolved = self.resolve_path(&p.path, generics);
2042                                                 let mangled_container = if let Some(ident) = self.crate_types.opaques.get(&resolved) {
2043                                                         format!("CVec_{}Z", ident)
2044                                                 } else if let Some(en) = self.crate_types.mirrored_enums.get(&resolved) {
2045                                                         format!("CVec_{}Z", en.ident)
2046                                                 } else if let Some(id) = p.path.get_ident() {
2047                                                         format!("CVec_{}Z", id)
2048                                                 } else { return false; };
2049                                                 write!(w, "{}::{}", Self::generated_container_path(), mangled_container).unwrap();
2050                                                 self.check_create_container(mangled_container, "Vec", vec![&*r.elem], generics, false)
2051                                         } else { false }
2052                                 } else if let syn::Type::Tuple(_) = &*s.elem {
2053                                         let mut args = syn::punctuated::Punctuated::new();
2054                                         args.push(syn::GenericArgument::Type((*s.elem).clone()));
2055                                         let mut segments = syn::punctuated::Punctuated::new();
2056                                         segments.push(syn::PathSegment {
2057                                                 ident: syn::Ident::new("Vec", Span::call_site()),
2058                                                 arguments: syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
2059                                                         colon2_token: None, lt_token: syn::Token![<](Span::call_site()), args, gt_token: syn::Token![>](Span::call_site()),
2060                                                 })
2061                                         });
2062                                         self.write_c_type_intern(w, &syn::Type::Path(syn::TypePath { qself: None, path: syn::Path { leading_colon: None, segments } }), generics, false, is_mut, ptr_for_ref)
2063                                 } else { false }
2064                         },
2065                         syn::Type::Tuple(t) => {
2066                                 if t.elems.len() == 0 {
2067                                         true
2068                                 } else {
2069                                         self.write_c_mangled_container_path(w, t.elems.iter().collect(), generics,
2070                                                 &format!("{}Tuple", t.elems.len()), is_ref, is_mut, ptr_for_ref)
2071                                 }
2072                         },
2073                         _ => false,
2074                 }
2075         }
2076         pub fn write_c_type<W: std::io::Write>(&mut self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, ptr_for_ref: bool) {
2077                 assert!(self.write_c_type_intern(w, t, generics, false, false, ptr_for_ref));
2078         }
2079         pub fn understood_c_path(&mut self, p: &syn::Path) -> bool {
2080                 if p.leading_colon.is_some() { return false; }
2081                 self.write_c_path_intern(&mut std::io::sink(), p, None, false, false, false)
2082         }
2083         pub fn understood_c_type(&mut self, t: &syn::Type, generics: Option<&GenericTypes>) -> bool {
2084                 self.write_c_type_intern(&mut std::io::sink(), t, generics, false, false, false)
2085         }
2086 }