2 -- | Computations in the vectorisation monad concerned with naming
3 -- and fresh variable generation.
4 module Vectorise.Monad.Naming
14 import Vectorise.Monad.Base
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)
32 occ_name = mk_occ (nameOccName name)
34 make u | isExternalName name = mkExternalName u (nameModule name)
37 | otherwise = mkSystemName u occ_name
40 -- | Clone an `Id`, using the provided function to transform its `OccName`.
41 cloneId :: (OccName -> OccName) -> Id -> Type -> VM Id
44 name <- cloneName mk_occ (getName id)
45 let id' | isExportedId id = Id.mkExportedLocalId name ty
46 | otherwise = Id.mkLocalId name ty
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)
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
61 let name = mkExternalName u mod occ_name noSrcSpan
63 return $ Id.mkExportedLocalId name ty
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
70 = do u <- liftDs newUnique
71 return $ mkSysLocal fs u ty
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)
80 -- | Make a new local dummy variable.
81 newDummyVar :: Type -> VM Var
82 newDummyVar = newLocalVar (fsLit "vv")
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
89 = do u <- liftDs newUnique
90 return $ mkTyVar (mkSysTvName u fs) k