Finish breaking up VectBuiltIn and VectMonad, and add comments
[ghc-hetmet.git] / compiler / vectorise / Vectorise / Monad / Naming.hs
1
2 -- | Computations in the vectorisation monad concerned with naming
3 --   and fresh variable generation.
4 module Vectorise.Monad.Naming
5         ( cloneName
6         , cloneId
7         , cloneVar
8         , newExportedVar
9         , newLocalVar
10         , newLocalVars
11         , newDummyVar
12         , newTyVar)
13 where
14 import Vectorise.Monad.Base
15
16 import DsMonad
17 import Type
18 import Var
19 import OccName
20 import Name
21 import SrcLoc
22 import Id
23 import FastString
24 import Control.Monad
25
26
27 -- Naming --------------------------------------------------------------------- 
28 -- | Clone a name, using the provide function to transform its `OccName`.       
29 cloneName :: (OccName -> OccName) -> Name -> VM Name
30 cloneName mk_occ name = liftM make (liftDs newUnique)
31   where
32     occ_name = mk_occ (nameOccName name)
33
34     make u | isExternalName name = mkExternalName u (nameModule name)
35                                                     occ_name
36                                                     (nameSrcSpan name)
37            | otherwise           = mkSystemName u occ_name
38
39
40 -- | Clone an `Id`, using the provided function to transform its `OccName`. 
41 cloneId :: (OccName -> OccName) -> Id -> Type -> VM Id
42 cloneId mk_occ id ty
43   = do
44       name <- cloneName mk_occ (getName id)
45       let id' | isExportedId id = Id.mkExportedLocalId name ty
46               | otherwise       = Id.mkLocalId         name ty
47       return id'
48
49
50 -- | Make a fresh instance of this var, with a new unique.
51 cloneVar :: Var -> VM Var
52 cloneVar var = liftM (setIdUnique var) (liftDs newUnique)
53
54
55 -- | Make a fresh exported variable with the given type.
56 newExportedVar :: OccName -> Type -> VM Var
57 newExportedVar occ_name ty 
58  = do mod <- liftDs getModuleDs
59       u   <- liftDs newUnique
60
61       let name = mkExternalName u mod occ_name noSrcSpan
62       
63       return $ Id.mkExportedLocalId name ty
64
65
66 -- | Make a fresh local variable with the given type.
67 --   The variable's name is formed using the given string as the prefix.
68 newLocalVar :: FastString -> Type -> VM Var
69 newLocalVar fs ty
70  = do u <- liftDs newUnique
71       return $ mkSysLocal fs u ty
72
73
74 -- | Make several fresh local varaiables with the given types.
75 --   The variable's names are formed using the given string as the prefix.
76 newLocalVars :: FastString -> [Type] -> VM [Var]
77 newLocalVars fs = mapM (newLocalVar fs)
78
79
80 -- | Make a new local dummy variable.
81 newDummyVar :: Type -> VM Var
82 newDummyVar = newLocalVar (fsLit "vv")
83
84
85 -- | Make a fresh type variable with the given kind.
86 --   The variable's name is formed using the given string as the prefix.
87 newTyVar :: FastString -> Kind -> VM Var
88 newTyVar fs k
89  = do u <- liftDs newUnique
90       return $ mkTyVar (mkSysTvName u fs) k
91