1-----------------------------------------------------------------------------
2-----------------------------------------------------------------------------
4-- Module : Language.PureScript.Renamer
5-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
8-- Maintainer : Phil Freeman <paf31@cantab.net>
9-- Stability : experimental
13-- Renaming pass that prevents shadowing of local identifiers.
15-----------------------------------------------------------------------------
17module Language.PureScript.Renamer (renameInModules) where
19import Control.Applicative
20import Control.Monad.State
22import Data.List (find)
24import qualified Data.Map as M
25import qualified Data.Set as S
27import Language.PureScript.CoreFn
28import Language.PureScript.Names
29import Language.PureScript.Traversals
31import qualified Language.PureScript.Constants as C
34-- The state object used in this module
36data RenameState = RenameState {
38 -- A map from names bound (in the input) to their names (in the output)
40 rsBoundNames :: M.Map Ident Ident
42 -- The set of names which have been used and are in scope in the output
44 , rsUsedNames :: S.Set Ident
47type Rename = State RenameState
49initState :: [Ident] -> RenameState
50initState scope = RenameState (M.fromList (zip scope scope)) (S.fromList scope)
53-- Runs renaming starting with a list of idents for the initial scope.
55runRename :: [Ident] -> Rename a -> a
56runRename scope = flip evalState (initState scope)
59-- Creates a new renaming scope using the current as a basis. Used to backtrack
60-- when leaving an Abs.
62newScope :: Rename a -> Rename a
70-- Adds a new scope entry for an ident. If the ident is already present, a new
71-- unique name is generated and stored.
73updateScope :: Ident -> Rename Ident
74updateScope i@(Ident name) | name == C.__unused = return i
77 let name' = case name `S.member` rsUsedNames scope of
80 newNames = [ Ident (runIdent name ++ "_" ++ show (i :: Int)) | i <- [1..] ]
81 Just newName = find (`S.notMember` rsUsedNames scope) newNames
84 modify $ \s -> s { rsBoundNames = M.insert name name' (rsBoundNames s)
85 , rsUsedNames = S.insert name' (rsUsedNames s)
90-- Finds the new name to use for an ident.
92lookupIdent :: Ident -> Rename Ident
93lookupIdent i@(Ident name) | name == C.__unused = return i
95 name' <- gets $ M.lookup name . rsBoundNames
97 Just name'' -> return name''
98 Nothing -> error $ "Rename scope is missing ident '" ++ show name ++ "'"
101-- Finds idents introduced by declarations.
103findDeclIdents :: [Bind Ann] -> [Ident]
104findDeclIdents = concatMap go
106 go (NonRec ident _) = [ident]
107 go (Rec ds) = map fst ds
110-- Renames within each declaration in a module.
112renameInModules :: [Module Ann] -> [Module Ann]
113renameInModules = map go
115 go :: Module Ann -> Module Ann
116 go m@(Module _ _ _ _ decls) = m { moduleDecls = renameInDecl' (findDeclIdents decls) `map` decls }
117 renameInDecl' :: [Ident] -> Bind Ann -> Bind Ann
118 renameInDecl' scope = runRename scope . renameInDecl True
121-- Renames within a declaration. isTopLevel is used to determine whether the
122-- declaration is a module member or appearing within a Let. At the top level
123-- declarations are not renamed or added to the scope (they should already have
124-- been added), whereas in a Let declarations are renamed if their name shadows
125-- another in the current scope.
127renameInDecl :: Bool -> Bind Ann -> Rename (Bind Ann)
128renameInDecl isTopLevel (NonRec name val) = do
129 name' <- if isTopLevel then return name else updateScope name
130 NonRec name' <$> renameInValue val
131renameInDecl isTopLevel (Rec ds) = do
132 ds' <- mapM updateNames ds
133 Rec <$> mapM updateValues ds'
135 updateNames :: (Ident, Expr Ann) -> Rename (Ident, Expr Ann)
136 updateNames (name, val) = do
137 name' <- if isTopLevel then return name else updateScope name
139 updateValues :: (Ident, Expr Ann) -> Rename (Ident, Expr Ann)
140 updateValues (name, val) = (,) name <$> renameInValue val
143-- Renames within a value.
145renameInValue :: Expr Ann -> Rename (Expr Ann)
146renameInValue (Literal ann l) =
147 Literal ann <$> renameInLiteral renameInValue l
148renameInValue c@(Constructor{}) = return c
149renameInValue (Accessor ann prop v) =
150 Accessor ann prop <$> renameInValue v
151renameInValue (ObjectUpdate ann obj vs) =
152 ObjectUpdate ann <$> renameInValue obj <*> mapM (\(name, v) -> (,) name <$> renameInValue v) vs
153renameInValue e@(Abs (_, _, _, Just IsTypeClassConstructor) _ _) = return e
154renameInValue (Abs ann name v) =
155 newScope $ Abs ann <$> updateScope name <*> renameInValue v
156renameInValue (App ann v1 v2) =
157 App ann <$> renameInValue v1 <*> renameInValue v2
158renameInValue (Var ann (Qualified Nothing name)) =
159 Var ann . Qualified Nothing <$> lookupIdent name
160renameInValue v@(Var{}) = return v
161renameInValue (Case ann vs alts) =
162 newScope $ Case ann <$> mapM renameInValue vs <*> mapM renameInCaseAlternative alts
163renameInValue (Let ann ds v) =
164 newScope $ Let ann <$> mapM (renameInDecl False) ds <*> renameInValue v
167-- Renames within literals.
169renameInLiteral :: (a -> Rename a) -> Literal a -> Rename (Literal a)
170renameInLiteral rename (ArrayLiteral bs) = ArrayLiteral <$> mapM rename bs
171renameInLiteral rename (ObjectLiteral bs) = ObjectLiteral <$> mapM (sndM rename) bs
172renameInLiteral _ l = return l
175-- Renames within case alternatives.
177renameInCaseAlternative :: CaseAlternative Ann -> Rename (CaseAlternative Ann)
178renameInCaseAlternative (CaseAlternative bs v) =
179 CaseAlternative <$> mapM renameInBinder bs
180 <*> eitherM (mapM (pairM renameInValue renameInValue)) renameInValue v
183-- Renames within binders.
185renameInBinder :: Binder a -> Rename (Binder a)
186renameInBinder n@(NullBinder{}) = return n
187renameInBinder (LiteralBinder ann b) =
188 LiteralBinder ann <$> renameInLiteral renameInBinder b
189renameInBinder (VarBinder ann name) =
190 VarBinder ann <$> updateScope name
191renameInBinder (ConstructorBinder ann tctor dctor bs) =
192 ConstructorBinder ann tctor dctor <$> mapM renameInBinder bs
193renameInBinder (NamedBinder ann name b) =
194 NamedBinder ann <$> updateScope name <*> renameInBinder b
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list