Perform topological sort of structs earlier during type checking
diff --git a/gen/src/mod.rs b/gen/src/mod.rs
index d19dea0..3d12c71 100644
--- a/gen/src/mod.rs
+++ b/gen/src/mod.rs
@@ -12,7 +12,6 @@
 mod namespace;
 mod nested;
 pub(super) mod out;
-mod toposort;
 mod write;
 
 pub(super) use self::error::Error;
diff --git a/gen/src/toposort.rs b/gen/src/toposort.rs
deleted file mode 100644
index 62a1148..0000000
--- a/gen/src/toposort.rs
+++ /dev/null
@@ -1,44 +0,0 @@
-use crate::syntax::{Api, Struct, Type, Types};
-use std::collections::btree_map::{BTreeMap as Map, Entry};
-
-enum Mark {
-    Visiting,
-    Visited,
-}
-
-pub fn sort<'a>(apis: &'a [Api], types: &Types<'a>) -> Vec<&'a Struct> {
-    let mut sorted = Vec::new();
-    let ref mut marks = Map::new();
-    for api in apis {
-        if let Api::Struct(strct) = api {
-            visit(strct, &mut sorted, marks, types);
-        }
-    }
-    sorted
-}
-
-fn visit<'a>(
-    strct: &'a Struct,
-    sorted: &mut Vec<&'a Struct>,
-    marks: &mut Map<*const Struct, Mark>,
-    types: &Types<'a>,
-) {
-    match marks.entry(strct) {
-        Entry::Occupied(entry) => match entry.get() {
-            Mark::Visiting => panic!("not a DAG"), // FIXME
-            Mark::Visited => return,
-        },
-        Entry::Vacant(entry) => {
-            entry.insert(Mark::Visiting);
-        }
-    }
-    for field in &strct.fields {
-        if let Type::Ident(ident) = &field.ty {
-            if let Some(inner) = types.structs.get(&ident.rust) {
-                visit(inner, sorted, marks, types);
-            }
-        }
-    }
-    marks.insert(strct, Mark::Visited);
-    sorted.push(strct);
-}
diff --git a/gen/src/write.rs b/gen/src/write.rs
index 414ea46..fd2a815 100644
--- a/gen/src/write.rs
+++ b/gen/src/write.rs
@@ -1,7 +1,7 @@
 use crate::gen::block::Block;
 use crate::gen::nested::NamespaceEntries;
 use crate::gen::out::OutFile;
-use crate::gen::{builtin, include, toposort, Opt};
+use crate::gen::{builtin, include, Opt};
 use crate::syntax::atom::Atom::{self, *};
 use crate::syntax::symbol::Symbol;
 use crate::syntax::{
@@ -96,7 +96,7 @@
         }
     }
 
-    for strct in toposort::sort(apis, out.types) {
+    for strct in &out.types.toposorted_structs {
         out.next_section();
         if !out.types.cxx.contains(&strct.name.rust) {
             write_struct(out, strct);