- mapRn_ (dupNamesErr doc_str) dups
- where
- (_, 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
- -> (Name -> Provenance)
- -> Avails -- Whats imported
- -> Deprecations
- -> GlobalRdrEnv
-
-mkGlobalRdrEnv this_mod unqual_imp 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 qualified names for the things that are available
- -- (Qualified names are always imported)
- gbl_env1 = foldl add_avail emptyRdrEnv avails
-
- -- Add unqualified names
- gbl_env2 | unqual_imp = foldl add_unqual gbl_env1 (rdrEnvToList gbl_env1)
- | otherwise = gbl_env1
-
- add_unqual env (qual_name, elts)
- = foldl add_one env elts
- where
- add_one env elt = addOneToGlobalRdrEnv env unqual_name elt
- unqual_name = unqualifyRdrName qual_name
- -- The qualified import should only have added one
- -- binding for each qualified name! But if there's an error in
- -- the module (multiple bindings for the same name) we may get
- -- duplicates. So the simple thing is to do the fold.
-
- add_avail :: GlobalRdrEnv -> AvailInfo -> GlobalRdrEnv
- add_avail env avail = foldl add_name env (availNames avail)
-
- add_name env name -- Add qualified name only
- = addOneToGlobalRdrEnv env (mkRdrQual this_mod occ) elt
- where
- occ = nameOccName name
- elt = GRE name (mk_provenance name) (lookupDeprec deprecs name)
-\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,
--- and the thing it is bound to is the name we are looking for
--- (i.e. false if A.f and B.f are both in scope as unqualified 'f')
---
--- This fn is only efficient if the shared
--- partial application is used a lot.
-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
-
-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
-
--------------------------------------
-filterAvail :: RdrNameIE -- Wanted
- -> AvailInfo -- Available
- -> Maybe AvailInfo -- Resulting available;
- -- Nothing if (any of the) wanted stuff isn't there
-
-filterAvail ie@(IEThingWith want wants) avail@(AvailTC n ns)
- | sub_names_ok = Just (AvailTC n (filter is_wanted ns))
- | otherwise = Nothing