1 {-# LANGUAGE TemplateHaskell #-}
2 module NormalizeTypes where
6 import qualified Control.Monad.Trans.Writer as Writer
7 import qualified Control.Monad.Trans.State as State
8 import qualified Data.Monoid as Monoid
9 import qualified Data.Accessor.Template
11 import qualified Data.Map as Map
16 import qualified UniqSupply
17 import qualified VarSet
18 import Outputable ( Outputable, showSDoc, ppr )
23 import VHDLTypes -- For TypeState
25 data TransformState = TransformState {
26 tsUniqSupply_ :: UniqSupply.UniqSupply
27 , tsBindings_ :: Map.Map CoreBndr CoreExpr
28 , tsNormalized_ :: VarSet.VarSet -- ^ The binders that have been normalized
29 , tsType_ :: TypeState
32 $( Data.Accessor.Template.deriveAccessors ''TransformState )
34 -- A session of multiple transformations over multiple expressions
35 type TransformSession = (State.State TransformState)
36 -- Wrap a writer around a TransformSession, to run a single transformation
37 -- over a single expression and track if the expression was changed.
38 type TransformMonad = Writer.WriterT Monoid.Any TransformSession
40 -- | Transforms a CoreExpr and keeps track if it has changed.
41 type Transform = CoreExpr -> TransformMonad CoreExpr
43 -- Finds the value of a global binding, if available
44 getGlobalBind :: CoreBndr -> TransformSession (Maybe CoreExpr)
45 getGlobalBind bndr = do
46 bindings <- getA tsBindings
47 return $ Map.lookup bndr bindings
49 -- Adds a new global binding with the given value
50 addGlobalBind :: CoreBndr -> CoreExpr -> TransformSession ()
51 addGlobalBind bndr expr = modA tsBindings (Map.insert bndr expr)
53 -- Returns a list of all global binders
54 getGlobalBinders :: TransformSession [CoreBndr]
56 bindings <- getA tsBindings
57 return $ Map.keys bindings