Skip to content

Commit d2c06e0

Browse files
committed
remove unneeded #includes; unscoped same-module values; clean up throw msg
1 parent ff96c52 commit d2c06e0

File tree

4 files changed

+28
-33
lines changed

4 files changed

+28
-33
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cabal.sandbox.config
1212
.test_modules/
1313
bower_components/
1414
node_modules
15-
pscpp.cabal
15+
*.cabal
1616
tmp/
1717
.stack-work/
1818
output

src/CodeGen/IL.hs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
5454
do
5555
let usedNames = concatMap getNames decls
5656
let mnLookup = renameImports usedNames imps
57-
ilImports <- traverse (importToIL mnLookup) . (\\ (mn : C.primModules)) . (\\ [mn]) $ ordNub $ map snd imps
58-
interfaceImport <- importToIL (renameImports [] [(emptyAnn, mn)]) mn
5957
let decls' = renameModules mnLookup decls
6058
ilDecls <- mapM (bindToIL ModuleDecl) decls'
6159
optimized <- traverse (traverse (optimize modName')) ilDecls
6260
let optimized' = concat optimized
6361
values = annotValue <$> optimized'
6462
foreigns' = identToIL <$> foreigns
6563
interface = interfaceSource modName values foreigns
66-
implHeader = implHeaderSource modName ilImports interfaceImport
67-
implFooter = implFooterSource modName foreigns
64+
imports = nub . concat $ importToIL <$> optimized'
65+
moduleHeader = importToIL' modName
66+
implHeader = implHeaderSource modName imports moduleHeader
67+
implFooter = implFooterSource (runModuleName mn) foreigns
6868
return $ (interface, foreigns', optimized', implHeader, implFooter)
6969
where
7070
modName = moduleNameToIL mn
@@ -102,24 +102,27 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
102102
then freshModuleName (i + 1) mn' used
103103
else newName
104104

105-
-- | Generates C++ code for a module import, binding the required module
106-
-- to the alternative
107-
importToIL :: M.Map ModuleName (Ann, ModuleName) -> ModuleName -> m Text
108-
importToIL mnLookup mn' = do
109-
let ((_, _, _, _), mnSafe) = fromMaybe (internalError "Missing value in mnLookup") $ M.lookup mn' mnLookup
110-
mname = moduleNameToIL mnSafe
111-
pure $ "#include \"" <> mname <> "/" <> mname <> ".h\"\n"
105+
-- | Generates IL code for a module import
106+
--
107+
importToIL :: AST -> [Text]
108+
importToIL = AST.everything (++) modRef
109+
where
110+
modRef (AST.Indexer _ (AST.Var _ _) (AST.Var _ mname))
111+
| not $ T.null mname = [importToIL' mname]
112+
modRef _ = []
113+
importToIL' :: Text -> Text
114+
importToIL' h = "#include \"" <> h <> "/" <> h <> ".h\"\n"
112115

113116
-- | Replaces the `ModuleName`s in the AST so that the generated code refers to
114117
-- the collision-avoiding renamed module imports.
115118
renameModules :: M.Map ModuleName (Ann, ModuleName) -> [Bind Ann] -> [Bind Ann]
116119
renameModules mnLookup binds =
117-
let (f, _, _) = everywhereOnValues id goExpr goBinder
120+
let (f, _, _) = everywhereOnValues id ilExpr goBinder
118121
in map f binds
119122
where
120-
goExpr :: Expr a -> Expr a
121-
goExpr (Var ann q) = Var ann (renameQual q)
122-
goExpr e = e
123+
ilExpr :: Expr a -> Expr a
124+
ilExpr (Var ann q) = Var ann (renameQual q)
125+
ilExpr e = e
123126
goBinder :: Binder a -> Binder a
124127
goBinder (ConstructorBinder ann q1 q2 bs) = ConstructorBinder ann (renameQual q1) (renameQual q2) bs
125128
goBinder b = b
@@ -229,18 +232,6 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
229232
unApp :: Expr Ann -> [Expr Ann] -> (Expr Ann, [Expr Ann])
230233
unApp (App _ val arg) args = unApp val (arg : args)
231234
unApp other args = (other, args)
232-
-- valueToIL (Var (_, _, _, Just IsForeign) qi@(Qualified (Just mn') ident)) =
233-
-- return $ if mn' == mn
234-
-- then AST.Var Nothing (moduleNameToIL mn' <> "::" <> identToIL ident)
235-
-- else varToIL qi
236-
-- valueToIL (Var (_, _, _, Just IsForeign) (Qualified (Just mn') ident)) =
237-
-- return $ AST.Var Nothing (moduleNameToIL mn' <> "::" <> identToIL ident)
238-
-- valueToIL (Var (_, _, _, Just IsForeign) qi@(Qualified (Just mn') ident)) =
239-
-- return $ if mn' == mn
240-
-- then foreignIdent ident
241-
-- else varToIL qi
242-
-- valueToIL (Var (_, _, _, Just IsForeign) ident) =
243-
-- internalError $ "Encountered an unqualified reference to a foreign ident " ++ T.unpack (showQualified showIdent ident)
244235
valueToIL (Var _ ident) = return $ varToIL ident
245236
valueToIL (Case (ss, _, _, _) values binders) = do
246237
vals <- mapM valueToIL values
@@ -300,9 +291,9 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
300291
-- | Generate code in the simplified intermediate representation for a reference to a
301292
-- variable that may have a qualified name.
302293
qualifiedToIL :: (a -> Ident) -> Qualified a -> AST
303-
qualifiedToIL f (Qualified (Just (ModuleName [ProperName mn'])) a) | mn' == C.prim = AST.Var Nothing . runIdent $ f a
304-
qualifiedToIL f (Qualified (Just mn') a) = AST.Indexer Nothing (AST.Var Nothing . identToIL $ f a) (AST.Var Nothing (moduleNameToIL mn'))
305-
qualifiedToIL f (Qualified _ a) = AST.Var Nothing $ identToIL (f a)
294+
qualifiedToIL f (Qualified (Just (ModuleName [ProperName mn'])) a) | mn' == C.prim = AST.Var Nothing . identToIL $ f a
295+
qualifiedToIL f (Qualified (Just mn') a) | mn /= mn' = AST.Indexer Nothing (AST.Var Nothing . identToIL $ f a) (AST.Var Nothing (moduleNameToIL mn'))
296+
qualifiedToIL f (Qualified _ a) = AST.Indexer Nothing (AST.Var Nothing . identToIL $ f a) (AST.Var Nothing "")
306297

307298
-- foreignIdent :: Ident -> AST
308299
-- foreignIdent ident = accessorString (mkString $ runIdent ident) (AST.Var Nothing "$foreign")
@@ -327,7 +318,7 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
327318
go _ _ _ = internalError "Invalid arguments to bindersToIL"
328319

329320
failedPatternMessage :: Text
330-
failedPatternMessage = "Failed pattern match at " <> runModuleName mn <> " " <> displayStartEndPos ss <> ": "
321+
failedPatternMessage = "Failed pattern match at " <> runModuleName mn <> " " <> displayStartEndPos ss
331322

332323
guardsToIL :: Either [(Guard Ann, Expr Ann)] (Expr Ann) -> m [AST]
333324
guardsToIL (Left gs) = traverse genGuard gs where

src/CodeGen/IL/Optimizer/TCO.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ tco mn = everywhere convert where
138138

139139
isSelfCall :: Text -> AST -> Bool
140140
isSelfCall ident (App _ (Var _ ident') _) = ident == ident'
141+
isSelfCall ident (App _ (Indexer _ (Var _ ident') (Var _ "")) _) = ident == ident'
141142
isSelfCall ident (App _ (Indexer _ (Var _ ident') mn') _) = mn' == mn && ident == ident'
142143
isSelfCall ident (App _ fn _) = isSelfCall ident fn
143144
isSelfCall _ _ = False

src/CodeGen/IL/Printer.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ literals = mkPattern' match'
167167
(captures, render)
168168
| name == Just tcoLoop = ("[&]", renderArgByVal)
169169
| otherwise = ("[=]", renderArg)
170+
match (Indexer _ (Var _ name) (Var _ "")) = mconcat <$> sequence
171+
[ prettyPrintIL' (Var Nothing name)
172+
, return $ emit "()"
173+
]
170174
match (Indexer _ prop@(Var _ name) val) = mconcat <$> sequence
171175
[ prettyPrintIL' val
172176
, return $ emit "::"
@@ -255,7 +259,6 @@ literals = mkPattern' match'
255259
-- match (Throw _ _) = return mempty
256260
match (Throw _ value) = mconcat <$> sequence
257261
[ return $ emit "THROW_("
258-
, return $ emit "\"PatternMatchFailure: \""
259262
, prettyPrintIL' value
260263
, return $ emit ")"
261264
]

0 commit comments

Comments
 (0)