- (_, dups) = removeDups (\(n1,l1) (n2,l2) -> n1 `compare` n2) rdr_names_w_loc
-\end{code}
-
-
-%************************************************************************
-%* *
-\subsection{GlobalRdrEnv}
-%* *
-%************************************************************************
-
-\begin{code}
-mkGlobalRdrEnv :: ModuleName -- Imported module (after doing the "as M" name change)
- -> Bool -- True <=> want unqualified import
- -> Bool -- True <=> want qualified import
- -> [AvailInfo] -- What's to be hidden (but only the unqualified
- -- version is hidden)
- -> (Name -> Provenance)
- -> Avails -- Whats imported and how
- -> Deprecations
- -> GlobalRdrEnv
-
-mkGlobalRdrEnv this_mod unqual_imp qual_imp hides
- mk_provenance avails deprecs
- = gbl_env2
- where
- -- Make the name environment. We're talking about a
- -- single module here, so there must be no name clashes.
- -- In practice there only ever will be if it's the module
- -- being compiled.
-
- -- Add the things that are available
- gbl_env1 = foldl add_avail emptyRdrEnv avails
-
- -- Delete things that are hidden
- gbl_env2 = foldl del_avail gbl_env1 hides
-
- add_avail :: GlobalRdrEnv -> AvailInfo -> GlobalRdrEnv
- add_avail env avail = foldl add_name env (availNames avail)
-
- add_name env name
- | qual_imp && unqual_imp = env3
- | unqual_imp = env2
- | qual_imp = env1
- | otherwise = env
- where
- env1 = addOneToGlobalRdrEnv env (mkRdrQual this_mod occ) elt
- env2 = addOneToGlobalRdrEnv env (mkRdrUnqual occ) elt
- env3 = addOneToGlobalRdrEnv env1 (mkRdrUnqual occ) elt
- occ = nameOccName name
- elt = GRE name (mk_provenance name) (lookupDeprec deprecs name)
-
- del_avail env avail = foldl delOneFromGlobalRdrEnv env rdr_names
- where
- rdr_names = map (mkRdrUnqual . nameOccName) (availNames avail)
-
-mkIfaceGlobalRdrEnv :: [(ModuleName,Avails)] -> GlobalRdrEnv
--- Used to construct a GlobalRdrEnv for an interface that we've
--- read from a .hi file. We can't construct the original top-level
--- environment because we don't have enough info, but we compromise
--- by making an environment from its exports
-mkIfaceGlobalRdrEnv m_avails
- = foldl add emptyRdrEnv m_avails
- where
- add env (mod,avails) = plusGlobalRdrEnv env (mkGlobalRdrEnv mod True False []
- (\n -> LocalDef) avails NoDeprecs)
- -- The NoDeprecs is a bit of a hack I suppose
-\end{code}
-
-\begin{code}
-plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
-plusGlobalRdrEnv env1 env2 = plusFM_C combine_globals env1 env2
-
-addOneToGlobalRdrEnv :: GlobalRdrEnv -> RdrName -> GlobalRdrElt -> GlobalRdrEnv
-addOneToGlobalRdrEnv env rdr_name name = addToFM_C combine_globals env rdr_name [name]
-
-delOneFromGlobalRdrEnv :: GlobalRdrEnv -> RdrName -> GlobalRdrEnv
-delOneFromGlobalRdrEnv env rdr_name = delFromFM env rdr_name
-
-combine_globals :: [GlobalRdrElt] -- Old
- -> [GlobalRdrElt] -- New
- -> [GlobalRdrElt]
-combine_globals ns_old ns_new -- ns_new is often short
- = foldr add ns_old ns_new
- where
- add n ns | any (is_duplicate n) ns_old = map (choose n) ns -- Eliminate duplicates
- | otherwise = n:ns
-
- choose n m | n `beats` m = n
- | otherwise = m
-
- (GRE n pn _) `beats` (GRE m pm _) = n==m && pn `hasBetterProv` pm
-
- is_duplicate :: GlobalRdrElt -> GlobalRdrElt -> Bool
- is_duplicate (GRE n1 LocalDef _) (GRE n2 LocalDef _) = False
- is_duplicate (GRE n1 _ _) (GRE n2 _ _) = n1 == n2
-\end{code}
-
-We treat two bindings of a locally-defined name as a duplicate,
-because they might be two separate, local defns and we want to report
-and error for that, {\em not} eliminate a duplicate.
-
-On the other hand, if you import the same name from two different
-import statements, we {\em do} want to eliminate the duplicate, not report
-an error.
-
-If a module imports itself then there might be a local defn and an imported
-defn of the same name; in this case the names will compare as equal, but
-will still have different provenances.
-
-
-@unQualInScope@ returns a function that takes a @Name@ and tells whether
-its unqualified name is in scope. This is put as a boolean flag in
-the @Name@'s provenance to guide whether or not to print the name qualified
-in error messages.
-
-\begin{code}
-unQualInScope :: GlobalRdrEnv -> Name -> Bool
--- True if 'f' is in scope, and has only one binding
--- (i.e. false if A.f and B.f are both in scope as unqualified 'f')
-unQualInScope env
- = (`elemNameSet` unqual_names)
- where
- unqual_names :: NameSet
- unqual_names = foldRdrEnv add emptyNameSet env
- add rdr_name [GRE name _ _] unquals | isUnqual rdr_name = addOneToNameSet unquals name
- add _ _ unquals = unquals
-\end{code}
-
-
-%************************************************************************
-%* *
-\subsection{Avails}
-%* *
-%************************************************************************
-
-\begin{code}
-plusAvail (Avail n1) (Avail n2) = Avail n1
-plusAvail (AvailTC n1 ns1) (AvailTC n2 ns2) = AvailTC n2 (nub (ns1 ++ ns2))
--- Added SOF 4/97
-#ifdef DEBUG
-plusAvail a1 a2 = pprPanic "RnEnv.plusAvail" (hsep [ppr a1,ppr a2])
-#endif
-
-addAvail :: AvailEnv -> AvailInfo -> AvailEnv
-addAvail avails avail = extendNameEnv_C plusAvail avails (availName avail) avail
-
-emptyAvailEnv = emptyNameEnv
-unitAvailEnv :: AvailInfo -> AvailEnv
-unitAvailEnv a = unitNameEnv (availName a) a
-
-plusAvailEnv :: AvailEnv -> AvailEnv -> AvailEnv
-plusAvailEnv = plusNameEnv_C plusAvail
-
-availEnvElts = nameEnvElts
-
-addAvailToNameSet :: NameSet -> AvailInfo -> NameSet
-addAvailToNameSet names avail = addListToNameSet names (availNames avail)
-
-availsToNameSet :: [AvailInfo] -> NameSet
-availsToNameSet avails = foldl addAvailToNameSet emptyNameSet avails
-
-availName :: GenAvailInfo name -> name
-availName (Avail n) = n
-availName (AvailTC n _) = n
-
-availNames :: GenAvailInfo name -> [name]
-availNames (Avail n) = [n]
-availNames (AvailTC n ns) = ns