git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Improve imports generation
commitb09e6251513532ecd7ab1fca925e62d199ea36c8 authorAndy Arvanitis <andyarvanitis@gmail.com> dateWed 3 Jul 2019, 20:31 (7 years ago) parent3d96915
2 files changed+15−16
src/CodeGen/IL.hs+10−14 src/CodeGen/IL/Printer.hs+5−2
src/CodeGen/IL.hs+10−14
@@ -40,7 +40,6 @@ import CodeGen.IL.Optimizer
4040import CodeGen.IL.Printer
4141
4242data DeclType = ModuleDecl | LetDecl | RecLetDecl deriving (Eq)
43−data ImportType = Imported | Ignored
4443
4544-- | Generate code in the simplified intermediate representation for all declarations in a
4645-- module.
@@ -55,9 +54,6 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
5554 do
5655 let usedNames = concatMap getNames decls
5756 let mnLookup = renameImports usedNames imps
58− imports <- traverse (importToIL Imported mnLookup) . (\\ (mn : C.primModules)) . (\\ [mn]) $ ordNub $ map snd imps
59− ignoredImports <- traverse (importToIL Ignored mnLookup) . delete (ModuleName [ProperName C.prim]) . (\\ (mn : C.primModules)) $ ordNub $ map snd imps
60− interfaceImport <- importToIL Imported (renameImports [] [(emptyAnn, mn)]) mn
6157 let decls' = renameModules mnLookup decls
6258 ilDecls <- mapM (bindToIL ModuleDecl) decls'
6359 optimized <- traverse (traverse (optimize modName')) ilDecls
@@ -65,7 +61,8 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
6561 values = annotValue <$> optimized'
6662 foreigns' = moduleIdentToIL <$> foreigns
6763 interface = interfaceSource modName values foreigns
68− implHeader = implHeaderSource modName (imports ++ ignoredImports) interfaceImport
64+ imports = nub . concat $ importToIL <$> optimized'
65+ implHeader = implHeaderSource modName imports ""
6966 implFooter = implFooterSource modName foreigns
7067 return $ (interface, foreigns', optimized', implHeader, implFooter)
7168 where
@@ -104,15 +101,14 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
104101 then freshModuleName (i + 1) mn' used
105102 else newName
106103
107− -- | Generates IL code for a module import, binding the required module
108− -- to the alternative
109− importToIL :: ImportType -> M.Map ModuleName (Ann, ModuleName) -> ModuleName -> m Text
110− importToIL t mnLookup mn' = do
111− let ((_, _, _, _), mnSafe) = fromMaybe (internalError "Missing value in mnLookup") $ M.lookup mn' mnLookup
112− mname = moduleNameToIL mnSafe
113− pure $ case t of
114− Imported -> "import \"" <> mname <> "\"\n"
115− Ignored -> "type _ = " <> mname <> ".IGNORE_UNUSED_IMPORTS\n"
104+ -- | Generates IL code for a module import
105+ --
106+ importToIL :: AST -> [Text]
107+ importToIL = AST.everything (++) modRef
108+ where
109+ modRef (AST.Indexer _ (AST.Var _ _) (AST.Var _ mname))
110+ | not $ T.null mname = [mname]
111+ modRef _ = []
116112
117113 -- | Replaces the `ModuleName`s in the AST so that the generated code refers to
118114 -- the collision-avoiding renamed module imports.
src/CodeGen/IL/Printer.hs+5−2
@@ -405,12 +405,15 @@ implHeaderSource mn imports _ =
405405 (if mn == "Main"
406406 then "import \"ffi\"\n"
407407 else "") <>
408− T.concat imports <> "\n" <>
409− "type IGNORE_UNUSED_IMPORTS = bool\n" <>
408+ "import (\n" <>
409+ (T.concat $ formatImport <$> imports) <> ")\n" <>
410410 "type _ = IGNORE_UNUSED_RUNTIME\n" <>
411411 (if mn == "Main"
412412 then "\ntype _ = ffi.Loader\n\n"
413413 else "\n")
414+ where
415+ formatImport :: Text -> Text
416+ formatImport s = " \"" <> s <> "\"\n"
414417
415418implFooterSource :: Text -> [Ident] -> Text
416419implFooterSource mn foreigns =