--- Bool determines shadowing as in importsFromLocalDecls.
--- UniqFM FixItem is the same as in importsFromLocalDecls.
---
--- Add the new locally-bound names one by one, checking for duplicates as
--- we do so. Remember that in Template Haskell the duplicates
--- might *already be* in the GlobalRdrEnv from higher up the module.
---
--- Also update the FixityEnv with the fixities for the names brought into scope.
---
--- Note that the return values are the extensions of the two inputs,
--- not the extras relative to them.
-extendRdrEnvRn :: Bool -> (GlobalRdrEnv, NameEnv FixItem)
- -> [AvailInfo] -> UniqFM (Located Fixity) -> RnM (GlobalRdrEnv, NameEnv FixItem)
-extendRdrEnvRn shadowP (rdr_env, fix_env) avails fixities =
- let -- if there is a fixity decl for the gre,
- -- add it to the fixity env
- extendFixEnv env gre =
- let name = gre_name gre
- occ = nameOccName name
- curKey = occNameFS occ in
- case lookupUFM fixities curKey of
- Nothing -> env
- Just (L _ fi) -> extendNameEnv env name (FixItem occ fi)
-
- (rdr_env_to_extend, extender) =
- if shadowP
- then -- when shadowing is on,
- -- (1) we need to remove the existing Unquals for the
- -- names we're extending the env with
- -- (2) but extending the env is simple
- let names = concatMap availNames avails
- new_occs = map nameOccName names
- trimmed_rdr_env = hideSomeUnquals rdr_env new_occs
- in
- (trimmed_rdr_env,
- \(cur_rdr_env, cur_fix_env) -> \gre ->
- return (extendGlobalRdrEnv cur_rdr_env gre,
- extendFixEnv cur_fix_env gre))
- else -- when shadowing is off,
- -- (1) we don't munge the incoming env
- -- (2) but we need to check for dups when extending
- (rdr_env,
- \(cur_rdr_env, cur_fix_env) -> \gre ->
- let gres = lookupGlobalRdrEnv cur_rdr_env (nameOccName (gre_name gre))
- in case filter isLocalGRE gres of -- Check for existing *local* defns
- dup_gre:_ -> do { addDupDeclErr (gre_name dup_gre) (gre_name gre)
- ; return (cur_rdr_env, cur_fix_env) }
- [] -> return (extendGlobalRdrEnv cur_rdr_env gre,
- extendFixEnv cur_fix_env gre))
- in foldlM extender (rdr_env_to_extend, fix_env) (gresFromAvails LocalDef avails)