X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2FcoreSyn%2FCoreSubst.lhs;h=e63b0363a8feff4fe719ff1087339df6f584c30a;hb=90f571e1c0cbc77dc6660ba61322b94a4274caea;hp=6a2255cf8726fced0948731f492ce452d9d59dd5;hpb=cac2aca1e1874e936f3ef15ca2a81a32c7863750;p=ghc-hetmet.git diff --git a/compiler/coreSyn/CoreSubst.lhs b/compiler/coreSyn/CoreSubst.lhs index 6a2255c..e63b036 100644 --- a/compiler/coreSyn/CoreSubst.lhs +++ b/compiler/coreSyn/CoreSubst.lhs @@ -16,7 +16,7 @@ module CoreSubst ( emptySubst, mkEmptySubst, mkSubst, substInScope, isEmptySubst, extendIdSubst, extendIdSubstList, extendTvSubst, extendTvSubstList, - extendSubstList, zapSubstEnv, + extendSubst, extendSubstList, zapSubstEnv, extendInScope, extendInScopeList, extendInScopeIds, isInScope, @@ -43,8 +43,9 @@ import UniqSupply import Maybes import Outputable import PprCore () -- Instances -import Util -import FastTypes +import FastString + +import Data.List \end{code} @@ -163,24 +164,26 @@ extendTvSubstList :: Subst -> [(TyVar,Type)] -> Subst extendTvSubstList (Subst in_scope ids tvs) prs = Subst in_scope ids (extendVarEnvList tvs prs) extendSubstList :: Subst -> [(Var,CoreArg)] -> Subst -extendSubstList subst [] - = subst -extendSubstList (Subst in_scope ids tvs) ((tv,Type ty):prs) - = ASSERT( isTyVar tv ) extendSubstList (Subst in_scope ids (extendVarEnv tvs tv ty)) prs -extendSubstList (Subst in_scope ids tvs) ((id,expr):prs) - = ASSERT( isId id ) extendSubstList (Subst in_scope (extendVarEnv ids id expr) tvs) prs +extendSubstList subst [] = subst +extendSubstList subst ((var,rhs):prs) = extendSubstList (extendSubst subst var rhs) prs + +extendSubst :: Subst -> Var -> CoreArg -> Subst +extendSubst (Subst in_scope ids tvs) tv (Type ty) + = ASSERT( isTyVar tv ) Subst in_scope ids (extendVarEnv tvs tv ty) +extendSubst (Subst in_scope ids tvs) id expr + = ASSERT( isId id ) Subst in_scope (extendVarEnv ids id expr) tvs lookupIdSubst :: Subst -> Id -> CoreExpr -lookupIdSubst (Subst in_scope ids tvs) v +lookupIdSubst (Subst in_scope ids _) v | not (isLocalId v) = Var v | Just e <- lookupVarEnv ids v = e | Just v' <- lookupInScope in_scope v = Var v' -- Vital! See Note [Extending the Subst] - | otherwise = WARN( True, ptext SLIT("CoreSubst.lookupIdSubst") <+> ppr v ) + | otherwise = WARN( True, ptext (sLit "CoreSubst.lookupIdSubst") <+> ppr v ) Var v lookupTvSubst :: Subst -> TyVar -> Type -lookupTvSubst (Subst _ ids tvs) v = lookupVarEnv tvs v `orElse` Type.mkTyVarTy v +lookupTvSubst (Subst _ _ tvs) v = lookupVarEnv tvs v `orElse` Type.mkTyVarTy v ------------------------------ isInScope :: Var -> Subst -> Bool @@ -207,9 +210,9 @@ Pretty printing, for debugging only \begin{code} instance Outputable Subst where ppr (Subst in_scope ids tvs) - = ptext SLIT(" braces (fsep (map ppr (varEnvElts (getInScopeVars in_scope)))) - $$ ptext SLIT(" IdSubst =") <+> ppr ids - $$ ptext SLIT(" TvSubst =") <+> ppr tvs + = ptext (sLit " braces (fsep (map ppr (varEnvElts (getInScopeVars in_scope)))) + $$ ptext (sLit " IdSubst =") <+> ppr ids + $$ ptext (sLit " TvSubst =") <+> ppr tvs <> char '>' \end{code} @@ -307,6 +310,7 @@ substRecBndrs subst bndrs substIdBndr :: Subst -- Substitution to use for the IdInfo -> Subst -> Id -- Substitition and Id to transform -> (Subst, Id) -- Transformed pair + -- NB: unfolding may be zapped substIdBndr rec_subst subst@(Subst in_scope env tvs) old_id = (Subst (in_scope `extendInScopeSet` new_id) new_env tvs, new_id) @@ -323,7 +327,8 @@ substIdBndr rec_subst subst@(Subst in_scope env tvs) old_id -- The lazy-set is because we're in a loop here, with -- rec_subst, when dealing with a mutually-recursive group new_id = maybeModifyIdInfo mb_new_info id2 - mb_new_info = substIdInfo rec_subst (idInfo id2) + mb_new_info = substIdInfo rec_subst id2 (idInfo id2) + -- NB: unfolding info may be zapped -- Extend the substitution if the unique has changed -- See the notes with substTyVarBndr for the delVarEnv @@ -365,7 +370,7 @@ clone_id rec_subst subst@(Subst in_scope env tvs) (old_id, uniq) where id1 = setVarUnique old_id uniq id2 = substIdType subst id1 - new_id = maybeModifyIdInfo (substIdInfo rec_subst (idInfo old_id)) id2 + new_id = maybeModifyIdInfo (substIdInfo rec_subst id2 (idInfo old_id)) id2 new_env = extendVarEnv env old_id (Var new_id) \end{code} @@ -387,7 +392,7 @@ substTyVarBndr (Subst in_scope id_env tv_env) tv -> (Subst in_scope' id_env tv_env', tv') substTy :: Subst -> Type -> Type -substTy (Subst in_scope id_env tv_env) ty +substTy (Subst in_scope _id_env tv_env) ty = Type.substTy (TvSubst in_scope tv_env) ty \end{code} @@ -400,7 +405,7 @@ substTy (Subst in_scope id_env tv_env) ty \begin{code} substIdType :: Subst -> Id -> Id -substIdType subst@(Subst in_scope id_env tv_env) id +substIdType subst@(Subst _ _ tv_env) id | isEmptyVarEnv tv_env || isEmptyVarSet (Type.tyVarsOfType old_ty) = id | otherwise = setIdType id (substTy subst old_ty) -- The tyVarsOfType is cheaper than it looks @@ -410,11 +415,11 @@ substIdType subst@(Subst in_scope id_env tv_env) id old_ty = idType id ------------------ -substIdInfo :: Subst -> IdInfo -> Maybe IdInfo +substIdInfo :: Subst -> Id -> IdInfo -> Maybe IdInfo -- Always zaps the unfolding, to save substitution work -substIdInfo subst info +substIdInfo subst new_id info | nothing_to_do = Nothing - | otherwise = Just (info `setSpecInfo` substSpec subst old_rules + | otherwise = Just (info `setSpecInfo` substSpec subst new_id old_rules `setWorkerInfo` substWorker subst old_wrkr `setUnfoldingInfo` noUnfolding) where @@ -430,7 +435,7 @@ substWorker :: Subst -> WorkerInfo -> WorkerInfo -- Seq'ing on the returned WorkerInfo is enough to cause all the -- substitutions to happen completely -substWorker subst NoWorker +substWorker _ NoWorker = NoWorker substWorker subst (HasWorker w a) = case lookupIdSubst subst w of @@ -441,25 +446,28 @@ substWorker subst (HasWorker w a) -- via postInlineUnconditionally, hence warning) ------------------ -substSpec :: Subst -> SpecInfo -> SpecInfo +substSpec :: Subst -> Id -> SpecInfo -> SpecInfo -substSpec subst spec@(SpecInfo rules rhs_fvs) +substSpec subst new_fn spec@(SpecInfo rules rhs_fvs) | isEmptySubst subst = spec | otherwise = seqSpecInfo new_rules `seq` new_rules where + new_name = idName new_fn new_rules = SpecInfo (map do_subst rules) (substVarSet subst rhs_fvs) do_subst rule@(BuiltinRule {}) = rule do_subst rule@(Rule { ru_bndrs = bndrs, ru_args = args, ru_rhs = rhs }) - = rule { ru_bndrs = bndrs', + = rule { ru_bndrs = bndrs', + ru_fn = new_name, -- Important: the function may have changed its name! ru_args = map (substExpr subst') args, ru_rhs = substExpr subst' rhs } where (subst', bndrs') = substBndrs subst bndrs ------------------ +substVarSet :: Subst -> VarSet -> VarSet substVarSet subst fvs = foldVarSet (unionVarSet . subst_fv subst) emptyVarSet fvs where