+slurpDecls decls
+ = -- First of all, get all the compulsory decls
+ slurp_compulsories decls `thenRn` \ decls1 ->
+
+ -- Next get the optional ones
+ closeDecls Optional decls1 `thenRn` \ decls2 ->
+
+ -- Finally get those deferred data type declarations
+ getDeferredDataDecls `thenRn` \ data_decls ->
+ mapRn rn_data_decl data_decls `thenRn` \ rn_data_decls ->
+
+ -- Done
+ returnRn (rn_data_decls ++ decls2)
+
+ where
+ -- The "slurp_compulsories" function is a loop that alternates
+ -- between slurping compulsory decls and slurping the instance
+ -- decls thus made relavant.
+ -- We *must* loop again here. Why? Two reasons:
+ -- (a) an instance decl will give rise to an unresolved dfun, whose
+ -- decl we must slurp to get its version number; that's the version
+ -- number for the whole instance decl. (And its unfolding might mention new
+ -- unresolved names.)
+ -- (b) an instance decl might give rise to a new unresolved class,
+ -- whose decl we must slurp, which might let in some new instance decls,
+ -- and so on. Example: instance Foo a => Baz [a] where ...
+ slurp_compulsories decls
+ = closeDecls Compulsory decls `thenRn` \ decls1 ->
+
+ -- Instance decls still pending?
+ getImportedInstDecls `thenRn` \ inst_decls ->
+ if null inst_decls then
+ -- No, none
+ returnRn decls1
+ else
+ -- Yes, there are some, so rename them and loop
+ traceRn (sep [ptext SLIT("Slurped"), int (length inst_decls), ptext SLIT("instance decls")])
+ `thenRn_`
+ mapRn rn_inst_decl inst_decls `thenRn` \ new_inst_decls ->
+ slurp_compulsories (new_inst_decls ++ decls1)
+\end{code}
+
+\begin{code}
+closeDecls :: Necessity
+ -> [RenamedHsDecl] -- Declarations got so far