3232#include < emscripten/val.h>
3333
3434#include < cstdlib>
35- #include < format>
36- #include < iostream>
3735#include < optional>
3836#include < sstream>
3937
4038#ifdef CXX_WITH_MLIR
4139#include < cxx/mlir/codegen.h>
4240#include < cxx/mlir/cxx_dialect.h>
41+ #include < cxx/mlir/cxx_dialect_conversions.h>
42+ #include < cxx/wasm32_wasi_toolchain.h>
4343#include < llvm/Support/raw_os_ostream.h>
4444#include < mlir/IR/MLIRContext.h>
4545#endif
@@ -72,6 +72,9 @@ struct WrappedUnit {
7272 std::unique_ptr<DiagnosticsClient> diagnosticsClient;
7373 std::unique_ptr<cxx::TranslationUnit> unit;
7474 val api;
75+ #ifdef CXX_WITH_MLIR
76+ std::unique_ptr<cxx::Toolchain> toolchain_;
77+ #endif
7578
7679 WrappedUnit (std::string source, std::string filename, val api = {})
7780 : api(api) {
@@ -84,6 +87,13 @@ struct WrappedUnit {
8487 }
8588
8689 unit->beginPreprocessing (std::move (source), std::move (filename));
90+
91+ #ifdef CXX_WITH_MLIR
92+ auto tc = std::make_unique<cxx::Wasm32WasiToolchain>(unit->preprocessor ());
93+ tc->initMemoryLayout ();
94+ tc->addPredefinedMacros ();
95+ toolchain_ = std::move (tc);
96+ #endif
8797 }
8898
8999 auto getUnitHandle () const -> std::intptr_t {
@@ -182,20 +192,25 @@ struct WrappedUnit {
182192
183193 unit->endPreprocessing ();
184194
185- unit->parse ();
195+ unit->parse (cxx::ParserConfiguration{
196+ .checkTypes = true ,
197+ });
186198
187199 co_return val{true };
188200 }
189201
190202 auto emitIR () -> std::string {
191203#ifdef CXX_WITH_MLIR
192204 mlir::MLIRContext context;
205+ context.disableMultithreading ();
193206 context.loadDialect <mlir::cxx::CxxDialect>();
194207
195208 cxx::Codegen codegen (context, unit.get ());
196209
197210 auto ir = codegen (unit->ast ());
198211
212+ (void )cxx::lowerToMLIR (ir.module );
213+
199214 mlir::OpPrintingFlags flags;
200215 flags.enableDebugInfo (true , true );
201216
@@ -204,9 +219,7 @@ struct WrappedUnit {
204219 ir.module ->print (os, flags);
205220 os.flush ();
206221
207- auto code = out.str ();
208-
209- return code;
222+ return out.str ();
210223#else
211224 return {};
212225#endif
0 commit comments