From ae142a94e13a46035e9bc4a0774904c4f1ad51ba Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Sat, 22 Mar 2008 18:34:07 +0000 Subject: [PATCH] Added support for uniquifying function arguments to the Rain passes --- frontends/RainPasses.hs | 47 ++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/frontends/RainPasses.hs b/frontends/RainPasses.hs index 37aa8b1..15a5714 100644 --- a/frontends/RainPasses.hs +++ b/frontends/RainPasses.hs @@ -121,25 +121,15 @@ uniquifyAndResolveVars = everywhereM (mk1M uniquifyAndResolveVars') A.ndNameType = A.ProcName, A.ndType = newProc, A.ndAbbrevMode = A.Original, A.ndPlacement = A.Unplaced} return $ A.Spec m (A.Specification m' n newProc) scope - where - --This function is like applying mapM to doFormals', but we need to let each doFormals' call in turn - --transform the scope of the formals. This could possibly be done by using a StateT monad with the scope, - --but this method works just as well: - doFormals :: Data t => [A.Formal] -> t -> PassM ([A.Formal],t) - doFormals [] s = return ([],s) - doFormals (f:fs) s = do (f',s') <- doFormals' f s - (fs',s'') <- doFormals fs s' - return ((f':fs'),s'') - doFormals' :: Data t => A.Formal -> t -> PassM (A.Formal,t) - doFormals' (A.Formal am t n) scope - = do n' <- makeNonce $ A.nameName n - let newName = (n {A.nameName = n'}) - let m = A.nameMeta n - defineName newName A.NameDef {A.ndMeta = m, A.ndName = n', A.ndOrigName = A.nameName n, - A.ndNameType = A.VariableName, A.ndType = (A.Declaration m t), - A.ndAbbrevMode = am, A.ndPlacement = A.Unplaced} - let scope' = everywhere (mkT $ replaceNameName (A.nameName n) n') scope - return (A.Formal am t newName, scope') + -- Functions: + uniquifyAndResolveVars' (A.Spec m (A.Specification m' n + (A.Function m'' funcMode retTypes params funcBody)) scope) + = do (params', funcBody') <- doFormals params funcBody + let newFunc = (A.Function m'' funcMode retTypes params' funcBody') + defineName n A.NameDef {A.ndMeta = m', A.ndName = A.nameName n, A.ndOrigName = A.nameName n, + A.ndNameType = A.FunctionName, A.ndType = newFunc, + A.ndAbbrevMode = A.Original, A.ndPlacement = A.Unplaced} + return $ A.Spec m (A.Specification m' n newFunc) scope -- replicator names have their types recorded later, but are -- uniquified and resolved here @@ -150,6 +140,25 @@ uniquifyAndResolveVars = everywhereM (mk1M uniquifyAndResolveVars') --Other: uniquifyAndResolveVars' s = return s + --This function is like applying mapM to doFormals', but we need to let each doFormals' call in turn + --transform the scope of the formals. This could possibly be done by using a StateT monad with the scope, + --but this method works just as well: + doFormals :: Data t => [A.Formal] -> t -> PassM ([A.Formal],t) + doFormals [] s = return ([],s) + doFormals (f:fs) s = do (f',s') <- doFormals' f s + (fs',s'') <- doFormals fs s' + return ((f':fs'),s'') + doFormals' :: Data t => A.Formal -> t -> PassM (A.Formal,t) + doFormals' (A.Formal am t n) scope + = do n' <- makeNonce $ A.nameName n + let newName = (n {A.nameName = n'}) + let m = A.nameMeta n + defineName newName A.NameDef {A.ndMeta = m, A.ndName = n', A.ndOrigName = A.nameName n, + A.ndNameType = A.VariableName, A.ndType = (A.Declaration m t), + A.ndAbbrevMode = am, A.ndPlacement = A.Unplaced} + let scope' = everywhere (mkT $ replaceNameName (A.nameName n) n') scope + return (A.Formal am t newName, scope') + -- | Helper function for a few of the passes. Replaces 'A.nameName' of a 'A.Name' if it matches a given 'String'. replaceNameName :: String -- ^ The variable name to be replaced.