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