From d852ad9fedb71f23b3aa70db0c7b43f6d6fd10e2 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Tue, 20 Jun 2017 13:57:46 -0700 Subject: Only emit each `import`ed module once. If the user imports a module along more than one path, we need to make sure we don't emit the code twice. I handle this by keeping a set of already-emitted modules. Down the line, a more robust code generation strategy for non-"rewriter" use cases would be handling this at the per-declaration level, and this logic wouldn't really be needed. --- source/slang/emit.cpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'source/slang/emit.cpp') diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 21704f6ff..c531a0a77 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -34,6 +34,8 @@ struct EmitContext // instead. Dictionary mapGLSLSourcePathToID; int glslSourceIDCount = 0; + + HashSet modulesAlreadyEmitted; }; // @@ -2662,16 +2664,24 @@ static void EmitDeclImpl(EmitContext* context, RefPtr decl, RefPtrimportedModuleDecl; + auto moduleDecl = importDecl->importedModuleDecl.Ptr(); - // TODO: do we need to modify the code generation environment at - // all when doing this recursive emit? - // - // TODO: what if we import the same module along two different - // paths? Probably need logic to avoid emitting the same - // module more than once. + // We might import the same module along two different paths, + // so we need to be careful to only emit each module once + // per output. + if(!context->modulesAlreadyEmitted.Contains(moduleDecl)) + { + // Add the module to our set before emitting it, just + // in case a circular reference would lead us to + // infinite recursion (but that shouldn't be allowed + // in the first place). + context->modulesAlreadyEmitted.Add(moduleDecl); - EmitDeclsInContainer(context, moduleDecl); + // TODO: do we need to modify the code generation environment at + // all when doing this recursive emit? + + EmitDeclsInContainer(context, moduleDecl); + } return; } -- cgit v1.2.3