2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1996-1998
6 This module contains "tidying" code for *nested* expressions, bindings, rules.
7 The code for *top-level* bindings is in TidyPgm.
11 tidyExpr, tidyVarOcc, tidyRule, tidyRules
14 #include "HsVersions.h"
24 import Name hiding (tidyNameOcc)
32 %************************************************************************
34 \subsection{Tidying expressions, rules}
36 %************************************************************************
41 -> (TidyEnv, CoreBind)
43 tidyBind env (NonRec bndr rhs)
44 = tidyLetBndr env (bndr,rhs) =: \ (env', bndr') ->
45 (env', NonRec bndr' (tidyExpr env' rhs))
47 tidyBind env (Rec prs)
48 = mapAccumL tidyLetBndr env prs =: \ (env', bndrs') ->
49 map (tidyExpr env') (map snd prs) =: \ rhss' ->
50 (env', Rec (zip bndrs' rhss'))
53 ------------ Expressions --------------
54 tidyExpr :: TidyEnv -> CoreExpr -> CoreExpr
55 tidyExpr env (Var v) = Var (tidyVarOcc env v)
56 tidyExpr env (Type ty) = Type (tidyType env ty)
57 tidyExpr env (Lit lit) = Lit lit
58 tidyExpr env (App f a) = App (tidyExpr env f) (tidyExpr env a)
59 tidyExpr env (Note n e) = Note (tidyNote env n) (tidyExpr env e)
60 tidyExpr env (Cast e co) = Cast (tidyExpr env e) (tidyType env co)
62 tidyExpr env (Let b e)
63 = tidyBind env b =: \ (env', b') ->
64 Let b' (tidyExpr env' e)
66 tidyExpr env (Case e b ty alts)
67 = tidyBndr env b =: \ (env', b) ->
68 Case (tidyExpr env e) b (tidyType env ty)
69 (map (tidyAlt b env') alts)
71 tidyExpr env (Lam b e)
72 = tidyBndr env b =: \ (env', b) ->
73 Lam b (tidyExpr env' e)
75 ------------ Case alternatives --------------
76 tidyAlt case_bndr env (con, vs, rhs)
77 = tidyBndrs env vs =: \ (env', vs) ->
78 (con, vs, tidyExpr env' rhs)
80 ------------ Notes --------------
81 tidyNote env note = note
83 ------------ Rules --------------
84 tidyRules :: TidyEnv -> [CoreRule] -> [CoreRule]
86 tidyRules env (rule : rules)
87 = tidyRule env rule =: \ rule ->
88 tidyRules env rules =: \ rules ->
91 tidyRule :: TidyEnv -> CoreRule -> CoreRule
92 tidyRule env rule@(BuiltinRule {}) = rule
93 tidyRule env rule@(Rule { ru_bndrs = bndrs, ru_args = args, ru_rhs = rhs,
94 ru_fn = fn, ru_rough = mb_ns })
95 = tidyBndrs env bndrs =: \ (env', bndrs) ->
96 map (tidyExpr env') args =: \ args ->
97 rule { ru_bndrs = bndrs, ru_args = args,
98 ru_rhs = tidyExpr env' rhs,
99 ru_fn = tidyNameOcc env fn,
100 ru_rough = map (fmap (tidyNameOcc env')) mb_ns }
104 %************************************************************************
106 \subsection{Tidying non-top-level binders}
108 %************************************************************************
111 tidyNameOcc :: TidyEnv -> Name -> Name
112 -- In rules and instances, we have Names, and we must tidy them too
113 -- Fortunately, we can lookup in the VarEnv with a name
114 tidyNameOcc (_, var_env) n = case lookupUFM var_env n of
118 tidyVarOcc :: TidyEnv -> Var -> Var
119 tidyVarOcc (_, var_env) v = lookupVarEnv var_env v `orElse` v
121 -- tidyBndr is used for lambda and case binders
122 tidyBndr :: TidyEnv -> Var -> (TidyEnv, Var)
124 | isTyVar var = tidyTyVarBndr env var
125 | otherwise = tidyIdBndr env var
127 tidyBndrs :: TidyEnv -> [Var] -> (TidyEnv, [Var])
128 tidyBndrs env vars = mapAccumL tidyBndr env vars
130 tidyLetBndr :: TidyEnv -> (Id, CoreExpr) -> (TidyEnv, Var)
131 -- Used for local (non-top-level) let(rec)s
132 tidyLetBndr env (id,rhs)
133 = ((tidy_env,new_var_env), final_id)
135 ((tidy_env,var_env), new_id) = tidyIdBndr env id
137 -- We need to keep around any interesting strictness and
138 -- demand info because later on we may need to use it when
139 -- converting to A-normal form.
141 -- f (g x), where f is strict in its argument, will be converted
142 -- into case (g x) of z -> f z by CorePrep, but only if f still
143 -- has its strictness info.
145 -- Similarly for the demand info - on a let binder, this tells
146 -- CorePrep to turn the let into a case.
148 -- Similarly arity info for eta expansion in CorePrep
150 -- Set inline-prag info so that we preseve it across
151 -- separate compilation boundaries
152 final_id = new_id `setIdInfo` new_info
154 new_info = vanillaIdInfo
155 `setArityInfo` exprArity rhs
156 `setAllStrictnessInfo` newStrictnessInfo idinfo
157 `setNewDemandInfo` newDemandInfo idinfo
158 `setInlinePragInfo` inlinePragInfo idinfo
160 -- Override the env we get back from tidyId with the new IdInfo
161 -- so it gets propagated to the usage sites.
162 new_var_env = extendVarEnv var_env id final_id
164 -- Non-top-level variables
165 tidyIdBndr :: TidyEnv -> Id -> (TidyEnv, Id)
166 tidyIdBndr env@(tidy_env, var_env) id
167 = -- do this pattern match strictly, otherwise we end up holding on to
168 -- stuff in the OccName.
169 case tidyOccName tidy_env (getOccName id) of { (tidy_env', occ') ->
171 -- Give the Id a fresh print-name, *and* rename its type
172 -- The SrcLoc isn't important now,
173 -- though we could extract it from the Id
175 -- All nested Ids now have the same IdInfo, namely vanillaIdInfo,
176 -- which should save some space.
177 -- But note that tidyLetBndr puts some of it back.
178 ty' = tidyType env (idType id)
179 id' = mkUserLocal occ' (idUnique id) ty' noSrcSpan
180 `setIdInfo` vanillaIdInfo
181 var_env' = extendVarEnv var_env id id'
183 ((tidy_env', var_env'), id')