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)
34 %************************************************************************
36 \subsection{Tidying expressions, rules}
38 %************************************************************************
43 -> (TidyEnv, CoreBind)
45 tidyBind env (NonRec bndr rhs)
46 = tidyLetBndr env (bndr,rhs) =: \ (env', bndr') ->
47 (env', NonRec bndr' (tidyExpr env' rhs))
49 tidyBind env (Rec prs)
50 = mapAccumL tidyLetBndr env prs =: \ (env', bndrs') ->
51 map (tidyExpr env') (map snd prs) =: \ rhss' ->
52 (env', Rec (zip bndrs' rhss'))
55 ------------ Expressions --------------
56 tidyExpr :: TidyEnv -> CoreExpr -> CoreExpr
57 tidyExpr env (Var v) = Var (tidyVarOcc env v)
58 tidyExpr env (Type ty) = Type (tidyType env ty)
59 tidyExpr env (Lit lit) = Lit lit
60 tidyExpr env (App f a) = App (tidyExpr env f) (tidyExpr env a)
61 tidyExpr env (Note n e) = Note (tidyNote env n) (tidyExpr env e)
62 tidyExpr env (Cast e co) = Cast (tidyExpr env e) (tidyType env co)
64 tidyExpr env (Let b e)
65 = tidyBind env b =: \ (env', b') ->
66 Let b' (tidyExpr env' e)
68 tidyExpr env (Case e b ty alts)
69 = tidyBndr env b =: \ (env', b) ->
70 Case (tidyExpr env e) b (tidyType env ty)
71 (map (tidyAlt b env') alts)
73 tidyExpr env (Lam b e)
74 = tidyBndr env b =: \ (env', b) ->
75 Lam b (tidyExpr env' e)
77 ------------ Case alternatives --------------
78 tidyAlt case_bndr env (con, vs, rhs)
79 = tidyBndrs env vs =: \ (env', vs) ->
80 (con, vs, tidyExpr env' rhs)
82 ------------ Notes --------------
83 tidyNote env note = note
85 ------------ Rules --------------
86 tidyRules :: TidyEnv -> [CoreRule] -> [CoreRule]
88 tidyRules env (rule : rules)
89 = tidyRule env rule =: \ rule ->
90 tidyRules env rules =: \ rules ->
93 tidyRule :: TidyEnv -> CoreRule -> CoreRule
94 tidyRule env rule@(BuiltinRule {}) = rule
95 tidyRule env rule@(Rule { ru_bndrs = bndrs, ru_args = args, ru_rhs = rhs,
96 ru_fn = fn, ru_rough = mb_ns })
97 = tidyBndrs env bndrs =: \ (env', bndrs) ->
98 map (tidyExpr env') args =: \ args ->
99 rule { ru_bndrs = bndrs, ru_args = args,
100 ru_rhs = tidyExpr env' rhs,
101 ru_fn = tidyNameOcc env fn,
102 ru_rough = map (fmap (tidyNameOcc env')) mb_ns }
106 %************************************************************************
108 \subsection{Tidying non-top-level binders}
110 %************************************************************************
113 tidyNameOcc :: TidyEnv -> Name -> Name
114 -- In rules and instances, we have Names, and we must tidy them too
115 -- Fortunately, we can lookup in the VarEnv with a name
116 tidyNameOcc (_, var_env) n = case lookupUFM var_env n of
120 tidyVarOcc :: TidyEnv -> Var -> Var
121 tidyVarOcc (_, var_env) v = lookupVarEnv var_env v `orElse` v
123 -- tidyBndr is used for lambda and case binders
124 tidyBndr :: TidyEnv -> Var -> (TidyEnv, Var)
126 | isTyVar var = tidyTyVarBndr env var
127 | otherwise = tidyIdBndr env var
129 tidyBndrs :: TidyEnv -> [Var] -> (TidyEnv, [Var])
130 tidyBndrs env vars = mapAccumL tidyBndr env vars
132 tidyLetBndr :: TidyEnv -> (Id, CoreExpr) -> (TidyEnv, Var)
133 -- Used for local (non-top-level) let(rec)s
134 tidyLetBndr env (id,rhs)
135 = ((tidy_env,new_var_env), final_id)
137 ((tidy_env,var_env), new_id) = tidyIdBndr env id
139 -- We need to keep around any interesting strictness and
140 -- demand info because later on we may need to use it when
141 -- converting to A-normal form.
143 -- f (g x), where f is strict in its argument, will be converted
144 -- into case (g x) of z -> f z by CorePrep, but only if f still
145 -- has its strictness info.
147 -- Similarly for the demand info - on a let binder, this tells
148 -- CorePrep to turn the let into a case.
150 -- Similarly arity info for eta expansion in CorePrep
152 -- Set inline-prag info so that we preseve it across
153 -- separate compilation boundaries
154 final_id = new_id `setIdInfo` new_info
156 new_info = vanillaIdInfo
157 `setArityInfo` exprArity rhs
158 `setAllStrictnessInfo` newStrictnessInfo idinfo
159 `setNewDemandInfo` newDemandInfo idinfo
160 `setInlinePragInfo` inlinePragInfo idinfo
162 -- Override the env we get back from tidyId with the new IdInfo
163 -- so it gets propagated to the usage sites.
164 new_var_env = extendVarEnv var_env id final_id
166 -- Non-top-level variables
167 tidyIdBndr :: TidyEnv -> Id -> (TidyEnv, Id)
168 tidyIdBndr env@(tidy_env, var_env) id
169 = -- do this pattern match strictly, otherwise we end up holding on to
170 -- stuff in the OccName.
171 case tidyOccName tidy_env (getOccName id) of { (tidy_env', occ') ->
173 -- Give the Id a fresh print-name, *and* rename its type
174 -- The SrcLoc isn't important now,
175 -- though we could extract it from the Id
177 -- All nested Ids now have the same IdInfo, namely vanillaIdInfo,
178 -- which should save some space.
179 -- But note that tidyLetBndr puts some of it back.
180 ty' = tidyType env (idType id)
181 id' = mkUserLocal occ' (idUnique id) ty' noSrcSpan
182 `setIdInfo` vanillaIdInfo
183 var_env' = extendVarEnv var_env id id'
185 ((tidy_env', var_env'), id')