+%*********************************************************
+%* *
+\subsection{Data type declarations are handled specially}
+%* *
+%*********************************************************
+
+Data type declarations get special treatment. If we import a data type decl
+with all its constructors, we end up importing all the types mentioned in
+the constructors' signatures, and hence {\em their} data type decls, and so on.
+In effect, we get the transitive closure of data type decls. Worse, this drags
+in tons on instance decls, and their unfoldings, and so on.
+
+If only the type constructor is mentioned, then all this is a waste of time.
+If any of the data constructors are mentioned then we really have to
+drag in the whole declaration.
+
+So when we import the type constructor for a @data@ or @newtype@ decl, we
+put it in the "deferred data/newtype decl" pile in Ifaces. Right at the end
+we slurp these decls, if they havn't already been dragged in by an occurrence
+of a constructor.
+
+\begin{code}
+getNonWiredDataDecl needed_name
+ version
+ avail@(AvailTC tycon_name _)
+ ty_decl@(TyData new_or_data context tycon tyvars condecls derivings pragmas src_loc)
+ | needed_name == tycon_name
+ && opt_PruneTyDecls
+ -- don't prune newtypes, as the code generator may
+ -- want to peer inside a newtype type constructor
+ -- (ClosureInfo.fun_result_ty is the culprit.)
+ && not (new_or_data == NewType)
+ && not (nameUnique needed_name `elem` cCallishTyKeys)
+ -- Hack! Don't prune these tycons whose constructors
+ -- the desugarer must be able to see when desugaring
+ -- a CCall. Ugh!
+
+ = -- Need the type constructor; so put it in the deferred set for now
+ getIfacesRn `thenRn` \ ifaces ->
+ let
+ Ifaces this_mod mod_map decls_fm slurped_names imp_names
+ unslurped_insts deferred_data_decls inst_mods = ifaces
+
+ new_ifaces = Ifaces this_mod mod_map decls_fm slurped_names imp_names
+ unslurped_insts new_deferred_data_decls inst_mods
+
+ no_constr_ty_decl = TyData new_or_data [] tycon tyvars [] derivings pragmas src_loc
+ new_deferred_data_decls = addToFM deferred_data_decls tycon_name no_constr_ty_decl
+ -- Nota bene: we nuke both the constructors and the context in the deferred decl.
+ -- If we don't nuke the context then renaming the deferred data decls can give
+ -- new unresolved names (for the classes). This could be handled, but there's
+ -- no point. If the data type is completely abstract then we aren't interested
+ -- its context.
+ in
+ setIfacesRn new_ifaces `thenRn_`
+ returnRn (AvailTC tycon_name [tycon_name], Nothing)
+
+ | otherwise
+ = -- Need a data constructor, so delete the data decl from the deferred set if it's there
+ getIfacesRn `thenRn` \ ifaces ->
+ let
+ Ifaces this_mod mod_map decls_fm slurped_names imp_names
+ unslurped_insts deferred_data_decls inst_mods = ifaces
+
+ new_ifaces = Ifaces this_mod mod_map decls_fm slurped_names imp_names
+ unslurped_insts new_deferred_data_decls inst_mods
+
+ new_deferred_data_decls = delFromFM deferred_data_decls tycon_name
+ in
+ setIfacesRn new_ifaces `thenRn_`
+ returnRn (avail, Just (TyD ty_decl))
+\end{code}
+
+\begin{code}
+getDeferredDataDecls :: RnMG [(Name, RdrNameTyDecl)]
+getDeferredDataDecls
+ = getIfacesRn `thenRn` \ (Ifaces _ _ _ _ _ _ deferred_data_decls _) ->
+ let
+ deferred_list = fmToList deferred_data_decls
+ trace_msg = hang (text "Slurping abstract data/newtype decls for: ")
+ 4 (ppr (map fst deferred_list))
+ in
+ traceRn trace_msg `thenRn_`
+ returnRn deferred_list
+\end{code}
+
+
+%*********************************************************
+%* *
+\subsection{Instance declarations are handled specially}
+%* *
+%*********************************************************
+
+\begin{code}
+getImportedInstDecls :: RnMG [(Module,RdrNameInstDecl)]