@@ -601,6 +601,32 @@ fn void Generator.emitAutoInit(Generator* gen, string_buffer.Buf* out, QualType
601601    }
602602}
603603
604+ fn void Generator.on_public_import_decl(void* arg, ImportDecl* i) {
605+     Generator* gen = arg;
606+     Decl* d = cast<Decl*>(i);
607+ 
608+     if (!d.isUsedPublic()) return;
609+ 
610+     u32 name_idx = d.getNameIdx();
611+     if (gen.imports.contains_idx(name_idx)) return;
612+ 
613+     gen.imports.add(name_idx);
614+     gen.header.print("#include \"%s.h\"\n", d.getName());
615+ }
616+ 
617+ fn void Generator.on_private_import_decl(void* arg, ImportDecl* i) {
618+     Generator* gen = arg;
619+     Decl* d = cast<Decl*>(i);
620+ 
621+     if (d.isUsedPublic()) return;
622+ 
623+     u32 name_idx = d.getNameIdx();
624+     if (gen.imports.contains_idx(name_idx)) return;
625+ 
626+     gen.imports.add(name_idx);
627+     gen.out.print("#include \"%s.h\"\n", d.getName());
628+ }
629+ 
604630fn void Generator.on_forward_structs(void* arg, Decl* d) {
605631    Generator* gen = arg;
606632
@@ -654,13 +680,6 @@ fn void Generator.emitGlobalDecl(Generator* gen, Decl* d) {
654680        }
655681        break;
656682    case Import:
657-         assert(gen.fast_build);
658-         ImportDecl* id = cast<ImportDecl*>(d);
659-         Module* dest = id.getDest();
660- 
661-         // put self include in .c file, otherwise put in header if used publicly
662-         string_buffer.Buf* out = gen.getBuf(dest != gen.mod && d.isUsedPublic());
663-         out.print("#include \"%s.h\"\n", dest.getName());
664683        break;
665684    case StructType:
666685        StructTypeDecl* std = cast<StructTypeDecl*>(d);
@@ -901,6 +920,14 @@ fn void Generator.create_interface_decls(void* arg, AST* a) {
901920    a.visitDecls(Generator.on_interface_decl, arg);
902921}
903922
923+ fn void Generator.on_public_ast_imports(void* arg, AST* a) {
924+     a.visitImports(Generator.on_public_import_decl, arg);
925+ }
926+ 
927+ fn void Generator.on_private_ast_imports(void* arg, AST* a) {
928+     a.visitImports(Generator.on_private_import_decl, arg);
929+ }
930+ 
904931fn void Generator.on_ast_structs(void* arg, AST* a) {
905932    //Generator* gen = arg;
906933    a.visitTypeDecls(Generator.on_forward_structs, arg);
@@ -1066,6 +1093,21 @@ fn void Generator.on_module(void* arg, Module* m) {
10661093        return;
10671094    }
10681095
1096+     if (gen.fast_build) {
1097+         // generate self include
1098+         out.print("#include \"%s.h\"\n", gen.mod_name);
1099+         // For now just iterate twice, possibly generating same include twice
1100+         // if used public and non-public by other ASTs
1101+ 
1102+         // collect all public includes, strip duplicates and emit
1103+         gen.imports.clear();
1104+         m.visitASTs(Generator.on_public_ast_imports, gen);
1105+ 
1106+         // collect all non-public includes, strip duplicates and emit
1107+         gen.imports.clear();
1108+         m.visitASTs(Generator.on_private_ast_imports, gen);
1109+     }
1110+ 
10691111    m.visitASTs(Generator.ast_clear_generated, gen);
10701112    // generate forward decls of structs
10711113    m.visitASTs(Generator.on_ast_structs, arg);
0 commit comments