diff --git a/flambeau.nim b/flambeau.nim index 3d5f41e..8676f71 100644 --- a/flambeau.nim +++ b/flambeau.nim @@ -36,8 +36,8 @@ export serialize # C++ Standard Library # ---------------------------------------------------------------- -import ./flambeau/cpp/std_cpp -export std_cpp +import ./flambeau/cpp/[std_cpp, emitters] +export std_cpp, emitters # Convenience helpers # ---------------------------------------------------------------- diff --git a/flambeau/cpp/emitters.nim b/flambeau/cpp/emitters.nim new file mode 100644 index 0000000..f31e2f9 --- /dev/null +++ b/flambeau/cpp/emitters.nim @@ -0,0 +1,13 @@ +# Special emit - https://github.com/nim-lang/Nim/blob/acf3715/compiler/ccgstmts.nim#L1489-L1495 + +template emitTypes*(typesection: static string): untyped = + ## Emit a C/C++ typesection + {.emit: "/*TYPESECTION*/\n" & typesection.} + +template emitGlobals*(globals: static string): untyped = + ## Emit a C/C++ global variable declaration + {.emit: "/*VARSECTION*/\n" & globals.} + +template emitIncludes*(includes: static string): untyped = + ## Emit a C/C++ global variable declaration + {.emit: "/*INCLUDESECTION*/\n" & includes.} diff --git a/proof_of_concepts/poc09_end_to_end.nim b/proof_of_concepts/poc09_end_to_end.nim index 89d6b8d..af9502a 100644 --- a/proof_of_concepts/poc09_end_to_end.nim +++ b/proof_of_concepts/poc09_end_to_end.nim @@ -5,13 +5,21 @@ import ../flambeau, std/[enumerate, strformat] -# Net is defined in poc_09_end_to_end_types.nim.hpp -# to work around https://github.com/nim-lang/Nim/issues/16664 -# which workarounds https://github.com/nim-lang/Nim/issues/4687 +# Inline C++ +# workaround https://github.com/nim-lang/Nim/issues/16664 +# and workaround https://github.com/nim-lang/Nim/issues/4687 + +emitTypes: + """ + struct Net: public torch::nn::Module { + torch::nn::Linear fc1{nullptr}; + torch::nn::Linear fc2{nullptr}; + torch::nn::Linear fc3{nullptr}; + }; + """ type Net - {.pure, importcpp, - header:"poc09_end_to_end_types.nim.hpp".} + {.pure, importcpp.} = object of Module fc1: Linear fc2: Linear diff --git a/proof_of_concepts/poc09_end_to_end_types.nim.hpp b/proof_of_concepts/poc09_end_to_end_types.nim.hpp deleted file mode 100644 index 6586198..0000000 --- a/proof_of_concepts/poc09_end_to_end_types.nim.hpp +++ /dev/null @@ -1,22 +0,0 @@ -// We need Linear{nullptr} in the codegen -// so we would like to cheat by inlining C++ -// -// type Net {.pure.} = object of Module -// -// fc1: Linear -// fc2: Linear -// fc3: Linear -// -// https://github.com/nim-lang/Nim/issues/4687 -// -// However -// due to https://github.com/nim-lang/Nim/issues/16664 -// it needs to be in its own file - -#include - -struct Net: public torch::nn::Module { - torch::nn::Linear fc1{nullptr}; - torch::nn::Linear fc2{nullptr}; - torch::nn::Linear fc3{nullptr}; -};