- 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
- -> Avails -- What's to be hidden
- -- I.e. import (imports - hides)
- -> Deprecations
- -> GlobalRdrEnv
-
-mkGlobalRdrEnv this_mod unqual_imp mk_provenance avails hides deprecs
- = gbl_env3
- 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
-
- -- Delete (qualified names of) things that are hidden
- gbl_env2 = foldl del_avail gbl_env1 hides
-
- -- Add unqualified names
- gbl_env3 | unqual_imp = foldl add_unqual gbl_env2 (rdrEnvToList gbl_env2)
- | otherwise = gbl_env2
-
- 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.
-
- del_avail env avail
- = foldl delOneFromGlobalRdrEnv env rdr_names
- where
- rdr_names = map (mkRdrQual this_mod . nameOccName)
- (availNames avail)
-
-
- 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)
-
-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
- (\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)