Move around a bunch of types.
[matthijs/master-project/cλash.git] / TranslatorTypes.hs
1 --
2 -- Simple module providing some types used by Translator. These are in a
3 -- separate module to prevent circular dependencies in Pretty for example.
4 --
5 module TranslatorTypes where
6
7 import qualified Control.Monad.State as State
8 import qualified HscTypes
9 import qualified Data.Map as Map
10 import FlattenTypes
11 import HsValueMap
12
13
14 -- | A map from a HsFunction identifier to various stuff we collect about a
15 --   function along the way.
16 type FuncMap  = Map.Map HsFunction FuncData
17
18 -- | Some stuff we collect about a function along the way.
19 data FuncData = FuncData {
20   flatFunc :: Maybe FlatFunction
21 }
22
23 data VHDLSession = VHDLSession {
24   coreMod   :: HscTypes.CoreModule, -- The current module
25   nameCount :: Int,             -- A counter that can be used to generate unique names
26   funcs     :: FuncMap          -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
27 }
28
29 -- | Add the function to the session
30 addFunc :: HsFunction -> VHDLState ()
31 addFunc hsfunc = do
32   fs <- State.gets funcs -- Get the funcs element from the session
33   let fs' = Map.insert hsfunc (FuncData Nothing) fs -- Insert function
34   State.modify (\x -> x {funcs = fs' })
35
36 -- | Find the given function in the current session
37 getFunc :: HsFunction -> VHDLState (Maybe FuncData)
38 getFunc hsfunc = do
39   fs <- State.gets funcs -- Get the funcs element from the session
40   return $ Map.lookup hsfunc fs
41
42 -- | Sets the FlatFunction for the given HsFunction in the given setting.
43 setFlatFunc :: HsFunction -> FlatFunction -> VHDLState ()
44 setFlatFunc hsfunc flatfunc = do
45   fs <- State.gets funcs -- Get the funcs element from the session
46   let fs'= Map.adjust (\d -> d { flatFunc = Just flatfunc }) hsfunc fs
47   State.modify (\x -> x {funcs = fs' })
48
49 getModule :: VHDLState HscTypes.CoreModule
50 getModule = State.gets coreMod -- Get the coreMod element from the session
51
52 type VHDLState = State.State VHDLSession
53
54 -- Makes the given name unique by appending a unique number.
55 -- This does not do any checking against existing names, so it only guarantees
56 -- uniqueness with other names generated by uniqueName.
57 uniqueName :: String -> VHDLState String
58 uniqueName name = do
59   count <- State.gets nameCount -- Get the funcs element from the session
60   State.modify (\s -> s {nameCount = count + 1})
61   return $ name ++ "_" ++ (show count)
62
63 -- vim: set ts=8 sw=2 sts=2 expandtab: