+filterImports2 iface decl_spec (Just (want_hiding, import_items)) all_avails
+ = do -- check for errors, convert RdrNames to Names
+ opt_indexedtypes <- doptM Opt_IndexedTypes
+ items1 <- mapM (lookup_lie opt_indexedtypes) import_items
+
+ let items2 :: [(LIE Name, AvailInfo)]
+ items2 = concat items1
+ -- NB the AvailInfo may have duplicates, and several items
+ -- for the same parent; e.g N(x) and N(y)
+
+ names = availsToNameSet (map snd items2)
+ keep n = not (n `elemNameSet` names)
+ pruned_avails = filterAvails keep all_avails
+ hiding_prov = Imported [ImpSpec { is_decl = decl_spec, is_item = ImpAll }]
+
+ gres | want_hiding = gresFromAvails hiding_prov pruned_avails
+ | otherwise = concatMap (gresFromIE decl_spec) items2
+
+ return (Just (want_hiding, map fst items2), mkGlobalRdrEnv gres)
+ where
+ -- This environment is how we map names mentioned in the import
+ -- list to the actual Name they correspond to, and the family
+ -- that the Name belongs to (an AvailInfo).
+ --
+ -- This env will have entries for data constructors too,
+ -- they won't make any difference because naked entities like T
+ -- in an import list map to TcOccs, not VarOccs.
+ occ_env :: OccEnv (Name,AvailInfo)
+ occ_env = mkOccEnv [ (nameOccName n, (n,a))
+ | a <- all_avails, n <- availNames a ]
+
+ lookup_lie :: Bool -> LIE RdrName -> TcRn [(LIE Name, AvailInfo)]
+ lookup_lie opt_indexedtypes (L loc ieRdr)
+ = do
+ stuff <- setSrcSpan loc $
+ case lookup_ie opt_indexedtypes ieRdr of
+ Failed err -> addErr err >> return []
+ Succeeded a -> return a
+ checkDodgyImport stuff
+ return [ (L loc ie, avail) | (ie,avail) <- stuff ]
+ where
+ -- Warn when importing T(..) if T was exported abstractly
+ checkDodgyImport stuff
+ | IEThingAll n <- ieRdr, (_, AvailTC _ [one]):_ <- stuff
+ = ifOptM Opt_WarnDodgyImports (addWarn (dodgyImportWarn n))
+ -- NB. use the RdrName for reporting the warning
+ checkDodgyImport _
+ = return ()
+
+ -- For each import item, we convert its RdrNames to Names,
+ -- and at the same time construct an AvailInfo corresponding
+ -- to what is actually imported by this item.
+ -- Returns Nothing on error.
+ -- We return a list here, because in the case of an import
+ -- item like C, if we are hiding, then C refers to *both* a
+ -- type/class and a data constructor.
+ lookup_ie :: Bool -> IE RdrName -> MaybeErr Message [(IE Name,AvailInfo)]
+ lookup_ie opt_indexedtypes ie
+ = let bad_ie = Failed (badImportItemErr iface decl_spec ie)
+
+ lookup_name rdrName =
+ case lookupOccEnv occ_env (rdrNameOcc rdrName) of
+ Nothing -> bad_ie
+ Just n -> return n
+ in
+ case ie of
+ IEVar n -> do
+ (name,avail) <- lookup_name n
+ return [(IEVar name, trimAvail avail name)]
+
+ IEThingAll tc -> do
+ (name,avail) <- lookup_name tc
+ return [(IEThingAll name, avail)]
+
+ IEThingAbs tc
+ | want_hiding -- hiding ( C )
+ -- Here the 'C' can be a data constructor
+ -- *or* a type/class, or even both
+ -> let tc_name = lookup_name tc
+ dc_name = lookup_name (setRdrNameSpace tc srcDataName)
+ in
+ case catMaybeErr [ tc_name, dc_name ] of
+ [] -> bad_ie
+ names -> return [ (IEThingAbs n, trimAvail av n)
+ | (n,av) <- names ]
+ | otherwise
+ -> do (name,avail) <- lookup_name tc
+ return [(IEThingAbs name, AvailTC name [name])]
+
+ IEThingWith n ns -> do
+ (name,avail) <- lookup_name n
+ case avail of
+ AvailTC nm subnames | nm == name -> do
+ let env = mkOccEnv [ (nameOccName s, s)
+ | s <- subnames ]
+ let mb_children = map (lookupOccEnv env . rdrNameOcc) ns
+ children <-
+ if any isNothing mb_children
+ then bad_ie
+ else return (catMaybes mb_children)
+ -- check for proper import of indexed types
+ when (not opt_indexedtypes && any isTyConName children) $
+ Failed (typeItemErr (head . filter isTyConName
+ $ children )
+ (text "in import list"))
+ return [(IEThingWith name children, AvailTC name (name:children))]
+
+ _otherwise -> bad_ie
+
+ _other -> Failed illegalImportItemErr
+ -- could be IEModuleContents, IEGroup, IEDoc, IEDocNamed
+ -- all errors.
+\end{code}
+
+%************************************************************************
+%* *
+ Import/Export Utils
+%* *
+%************************************************************************
+
+\begin{code}
+-- | make a 'GlobalRdrEnv' where all the elements point to the same
+-- import declaration (useful for "hiding" imports, or imports with
+-- no details).
+gresFromAvails :: Provenance -> [AvailInfo] -> [GlobalRdrElt]
+gresFromAvails prov avails
+ = concatMap (gresFromAvail (const prov)) avails
+
+gresFromAvail :: (Name -> Provenance) -> AvailInfo -> [GlobalRdrElt]
+gresFromAvail prov_fn avail
+ = [ GRE {gre_name = n,
+ gre_par = availParent n avail,
+ gre_prov = prov_fn n}
+ | n <- availNames avail ]
+
+greAvail :: GlobalRdrElt -> AvailInfo
+greAvail gre = mkUnitAvail (gre_name gre) (gre_par gre)
+
+mkUnitAvail :: Name -> Parent -> AvailInfo
+mkUnitAvail me (ParentIs p) = AvailTC p [me]
+mkUnitAvail me NoParent | isTyConName me = AvailTC me [me]
+ | otherwise = Avail me
+
+plusAvail (Avail n1) (Avail n2) = Avail n1
+plusAvail (AvailTC n1 ns1) (AvailTC n2 ns2) = AvailTC n2 (ns1 `unionLists` ns2)
+plusAvail a1 a2 = pprPanic "RnEnv.plusAvail" (hsep [ppr a1,ppr a2])
+
+availParent :: Name -> AvailInfo -> Parent
+availParent n (Avail _) = NoParent
+availParent n (AvailTC m ms) | n==m = NoParent
+ | otherwise = ParentIs m
+
+trimAvail :: AvailInfo -> Name -> AvailInfo
+trimAvail (Avail n) m = Avail n
+trimAvail (AvailTC n ns) m = ASSERT( m `elem` ns) AvailTC n [m]
+
+-- | filters 'AvailInfo's by the given predicate
+filterAvails :: (Name -> Bool) -> [AvailInfo] -> [AvailInfo]
+filterAvails keep avails = foldr (filterAvail keep) [] avails
+
+-- | filters an 'AvailInfo' by the given predicate
+filterAvail :: (Name -> Bool) -> AvailInfo -> [AvailInfo] -> [AvailInfo]
+filterAvail keep ie rest =
+ case ie of
+ Avail n | keep n -> ie : rest
+ | otherwise -> rest
+ AvailTC tc ns ->
+ let left = filter keep ns in
+ if null left then rest else AvailTC tc left : rest
+
+-- | Given an import/export spec, construct the appropriate 'GlobalRdrElt's.
+gresFromIE :: ImpDeclSpec -> (LIE Name, AvailInfo) -> [GlobalRdrElt]
+gresFromIE decl_spec (L loc ie, avail)
+ = gresFromAvail prov_fn avail
+ where
+ is_explicit = case ie of
+ IEThingAll name -> \n -> n==name
+ other -> \n -> True
+ prov_fn name = Imported [imp_spec]
+ where
+ imp_spec = ImpSpec { is_decl = decl_spec, is_item = item_spec }
+ item_spec = ImpSome { is_explicit = is_explicit name, is_iloc = loc }
+
+mkChildEnv :: [GlobalRdrElt] -> NameEnv [Name]
+mkChildEnv gres = foldr add emptyNameEnv gres
+ where
+ add (GRE { gre_name = n, gre_par = ParentIs p }) env = extendNameEnv_C (++) env p [n]
+ add other_gre env = env
+
+findChildren :: NameEnv [Name] -> Name -> [Name]
+findChildren env n = lookupNameEnv env n `orElse` []
+\end{code}
+
+---------------------------------------
+ AvailEnv and friends
+
+All this AvailEnv stuff is hardly used; only in a very small
+part of RnNames. Todo: remove?
+---------------------------------------
+
+\begin{code}
+type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
+
+emptyAvailEnv :: AvailEnv
+emptyAvailEnv = emptyNameEnv
+
+unitAvailEnv :: AvailInfo -> AvailEnv
+unitAvailEnv a = unitNameEnv (availName a) a
+
+plusAvailEnv :: AvailEnv -> AvailEnv -> AvailEnv
+plusAvailEnv = plusNameEnv_C plusAvail
+
+availEnvElts :: AvailEnv -> [AvailInfo]
+availEnvElts = nameEnvElts
+
+addAvail :: AvailEnv -> AvailInfo -> AvailEnv
+addAvail avails avail = extendNameEnv_C plusAvail avails (availName avail) avail
+
+mkAvailEnv :: [AvailInfo] -> AvailEnv
+ -- 'avails' may have several items with the same availName
+ -- E.g import Ix( Ix(..), index )
+ -- will give Ix(Ix,index,range) and Ix(index)
+ -- We want to combine these; addAvail does that
+mkAvailEnv avails = foldl addAvail emptyAvailEnv avails
+
+-- After combining the avails, we need to ensure that the parent name is the
+-- first entry in the list of subnames, if it is included at all. (Subsequent
+-- functions rely on that.)
+normaliseAvail :: AvailInfo -> AvailInfo
+normaliseAvail avail@(Avail _) = avail
+normaliseAvail (AvailTC name subs) = AvailTC name subs'
+ where
+ subs' = if name `elem` subs then name : (delete name subs) else subs
+
+-- | combines 'AvailInfo's from the same family
+nubAvails :: [AvailInfo] -> [AvailInfo]
+nubAvails avails = map normaliseAvail . nameEnvElts . mkAvailEnv $ avails