@@ -54,17 +54,17 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
54
54
do
55
55
let usedNames = concatMap getNames decls
56
56
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
59
57
let decls' = renameModules mnLookup decls
60
58
ilDecls <- mapM (bindToIL ModuleDecl ) decls'
61
59
optimized <- traverse (traverse (optimize modName')) ilDecls
62
60
let optimized' = concat optimized
63
61
values = annotValue <$> optimized'
64
62
foreigns' = identToIL <$> foreigns
65
63
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
68
68
return $ (interface, foreigns', optimized', implHeader, implFooter)
69
69
where
70
70
modName = moduleNameToIL mn
@@ -102,24 +102,27 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
102
102
then freshModuleName (i + 1 ) mn' used
103
103
else newName
104
104
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 "
112
115
113
116
-- | Replaces the `ModuleName`s in the AST so that the generated code refers to
114
117
-- the collision-avoiding renamed module imports.
115
118
renameModules :: M. Map ModuleName (Ann , ModuleName ) -> [Bind Ann ] -> [Bind Ann ]
116
119
renameModules mnLookup binds =
117
- let (f, _, _) = everywhereOnValues id goExpr goBinder
120
+ let (f, _, _) = everywhereOnValues id ilExpr goBinder
118
121
in map f binds
119
122
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
123
126
goBinder :: Binder a -> Binder a
124
127
goBinder (ConstructorBinder ann q1 q2 bs) = ConstructorBinder ann (renameQual q1) (renameQual q2) bs
125
128
goBinder b = b
@@ -229,18 +232,6 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
229
232
unApp :: Expr Ann -> [Expr Ann ] -> (Expr Ann , [Expr Ann ])
230
233
unApp (App _ val arg) args = unApp val (arg : args)
231
234
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)
244
235
valueToIL (Var _ ident) = return $ varToIL ident
245
236
valueToIL (Case (ss, _, _, _) values binders) = do
246
237
vals <- mapM valueToIL values
@@ -300,9 +291,9 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
300
291
-- | Generate code in the simplified intermediate representation for a reference to a
301
292
-- variable that may have a qualified name.
302
293
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 " " )
306
297
307
298
-- foreignIdent :: Ident -> AST
308
299
-- foreignIdent ident = accessorString (mkString $ runIdent ident) (AST.Var Nothing "$foreign")
@@ -327,7 +318,7 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
327
318
go _ _ _ = internalError " Invalid arguments to bindersToIL"
328
319
329
320
failedPatternMessage :: Text
330
- failedPatternMessage = " Failed pattern match at " <> runModuleName mn <> " " <> displayStartEndPos ss <> " : "
321
+ failedPatternMessage = " Failed pattern match at " <> runModuleName mn <> " " <> displayStartEndPos ss
331
322
332
323
guardsToIL :: Either [(Guard Ann , Expr Ann )] (Expr Ann ) -> m [AST ]
333
324
guardsToIL (Left gs) = traverse genGuard gs where
0 commit comments