2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1995
4 \section[MkIface]{Print an interface for a module}
7 #include "HsVersions.h"
23 import Bag ( emptyBag, snocBag, bagToList )
24 import Class ( GenClass(..){-instance NamedThing-}, GenClassOp(..) )
25 import CmdLineOpts ( opt_ProduceHi )
26 import FieldLabel ( FieldLabel{-instance NamedThing-} )
28 import Id ( idType, dataConSig, dataConFieldLabels,
29 dataConStrictMarks, StrictnessMark(..),
30 GenId{-instance NamedThing/Outputable-}
32 import Name ( nameOrigName, origName, nameOf,
33 exportFlagOn, nameExportFlag, ExportFlag(..),
34 ltLexical, isExported, getExportFlag,
35 isLexSym, isLocallyDefined,
36 RdrName(..){-instance Outputable-},
37 Name{-instance NamedThing-}
39 import ParseUtils ( UsagesMap(..), VersionsMap(..) )
40 import PprEnv -- not sure how much...
41 import PprStyle ( PprStyle(..) )
42 import PprType -- most of it (??)
43 import Pretty -- quite a bit
44 import RnHsSyn ( RenamedHsModule(..), RnName{-instance NamedThing-} )
45 import TcModule ( TcIfaceInfo(..) )
46 import TcInstUtil ( InstInfo(..) )
47 import TyCon ( TyCon(..){-instance NamedThing-}, NewOrData(..) )
48 import Type ( mkSigmaTy, mkDictTy, getAppTyCon )
49 import Util ( sortLt, zipWithEqual, zipWith3Equal, assertPanic, panic{-ToDo:rm-}, pprTrace{-ToDo:rm-} )
51 ppSemid x = ppBeside (ppr PprInterface x) ppSemi -- micro util
52 ppr_ty ty = pprType PprInterface ty
53 ppr_tyvar tv = ppr PprInterface tv
58 pp = ppr PprInterface on
60 (if isLexSym s then ppParens else id) pp
67 (if isLexSym s then ppParens else id) pp
70 We have a function @startIface@ to open the output file and put
71 (something like) ``interface Foo N'' in it. It gives back a handle
72 for subsequent additions to the interface file.
74 We then have one-function-per-block-of-interface-stuff, e.g.,
75 @ifaceExportList@ produces the @__exports__@ section; it appends
76 to the handle provided by @startIface@.
80 -> IO (Maybe Handle) -- Nothing <=> don't do an interface
81 endIface :: Maybe Handle -> IO ()
102 ifaceDecls :: Maybe Handle
103 -> TcIfaceInfo -- info produced by typechecker, for interfaces
107 -> TcIfaceInfo -- as above
112 ifacePragmas = panic "ifacePragmas" -- stub
117 = case opt_ProduceHi of
118 Nothing -> return Nothing -- not producing any .hi file
120 openFile fn WriteMode >>= \ if_hdl ->
121 hPutStr if_hdl ("interface "++ _UNPK_ mod ++" 1\n") >>
124 endIface Nothing = return ()
125 endIface (Just if_hdl) = hPutStr if_hdl "\n" >> hClose if_hdl
129 ifaceUsages Nothing{-no iface handle-} _ = return ()
131 ifaceUsages (Just if_hdl) version_info
132 = hPutStr if_hdl "__usages__\nFoo 1" -- a stub, obviously
136 ifaceVersions Nothing{-no iface handle-} _ = return ()
138 ifaceVersions (Just if_hdl) version_info
139 = hPutStr if_hdl "\n__versions__\nFoo 1" -- a stub, obviously
143 ifaceInstanceModules Nothing{-no iface handle-} _ = return ()
144 ifaceInstanceModules (Just _) [] = return ()
146 ifaceInstanceModules (Just if_hdl) imods
147 = hPutStr if_hdl "\n__instance_modules__\n" >>
148 hPutStr if_hdl (ppShow 100 (ppCat (map ppPStr imods)))
151 Export list: grab the Names of things that are marked Exported, sort
152 (so the interface file doesn't ``wobble'' from one compilation to the
153 next...), and print. Note that the ``module'' now contains all the
154 imported things that we are dealing with, thus including any entities
155 that we are re-exporting from somewhere else.
157 ifaceExportList Nothing{-no iface handle-} _ = return ()
159 ifaceExportList (Just if_hdl)
160 (HsModule _ _ _ _ _ typedecls _ classdecls _ _ _ binds sigs _)
162 name_flag_pairs :: Bag (Name, ExportFlag)
167 (from_binds binds emptyBag{-init accum-})
172 sorted_pairs = sortLt lexical_lt (bagToList name_flag_pairs)
175 hPutStr if_hdl "\n__exports__\n" >>
176 hPutStr if_hdl (ppShow 100 (ppAboves (map pp_pair sorted_pairs)))
178 from_ty (TyData _ n _ _ _ _ _) acc = maybe_add acc n
179 from_ty (TyNew _ n _ _ _ _ _) acc = maybe_add acc n
180 from_ty (TySynonym n _ _ _) acc = maybe_add acc n
182 from_cls (ClassDecl _ n _ _ _ _ _) acc = maybe_add acc n
184 from_sig (Sig n _ _ _) acc = maybe_add acc n
186 from_binds bs acc = maybe_add_list acc (collectTopLevelBinders bs)
189 maybe_add :: Bag (Name, ExportFlag) -> RnName -> Bag (Name, ExportFlag)
192 | exportFlagOn ef = acc `snocBag` (n, ef)
196 ef = nameExportFlag n
199 maybe_add_list acc [] = acc
200 maybe_add_list acc (n:ns) = maybe_add (maybe_add_list acc ns) n
203 lexical_lt (n1,_) (n2,_) = nameOrigName n1 < nameOrigName n2
207 = ppBeside (ppr_name n) (pp_export ef)
209 pp_export ExportAll = ppPStr SLIT("(..)")
210 pp_export ExportAbs = ppNil
214 ifaceFixities Nothing{-no iface handle-} _ = return ()
216 ifaceFixities (Just if_hdl) (HsModule _ _ _ _ fixities _ _ _ _ _ _ _ _ _)
218 local_fixities = filter from_here fixities
220 if null local_fixities then
223 hPutStr if_hdl "\n__fixities__\n" >>
224 hPutStr if_hdl (ppShow 100 (ppAboves (map ppSemid local_fixities)))
226 from_here (InfixL v _) = isLocallyDefined v
227 from_here (InfixR v _) = isLocallyDefined v
228 from_here (InfixN v _) = isLocallyDefined v
232 ifaceDecls Nothing{-no iface handle-} _ = return ()
234 ifaceDecls (Just if_hdl) (vals, tycons, classes, _)
236 exported_classes = filter isExported classes
237 exported_tycons = filter isExported tycons
238 exported_vals = filter isExported vals
240 sorted_classes = sortLt ltLexical exported_classes
241 sorted_tycons = sortLt ltLexical exported_tycons
242 sorted_vals = sortLt ltLexical exported_vals
244 ASSERT(not (null exported_classes && null exported_tycons && null exported_vals))
246 hPutStr if_hdl "\n__declarations__\n" >>
247 hPutStr if_hdl (ppShow 100 (ppAboves [
248 ppAboves (map ppr_class sorted_classes),
249 ppAboves (map ppr_tycon sorted_tycons),
250 ppAboves [ppr_val v (idType v) | v <- sorted_vals]]))
254 ifaceInstances Nothing{-no iface handle-} _ = return ()
256 ifaceInstances (Just if_hdl) (_, _, _, insts)
258 exported_insts = filter is_exported_inst (bagToList insts)
260 sorted_insts = sortLt lt_inst exported_insts
262 if null exported_insts then
265 hPutStr if_hdl "\n__instances__\n" >>
266 hPutStr if_hdl (ppShow 100 (ppAboves (map pp_inst sorted_insts)))
268 is_exported_inst (InstInfo clas _ ty _ _ _ _ _ from_here _ _ _)
269 = from_here -- && ...
272 lt_inst (InstInfo clas1 _ ty1 _ _ _ _ _ _ _ _ _)
273 (InstInfo clas2 _ ty2 _ _ _ _ _ _ _ _ _)
275 tycon1 = fst (getAppTyCon ty1)
276 tycon2 = fst (getAppTyCon ty2)
278 case (origName clas1 `cmp` origName clas2) of
281 EQ_ -> origName tycon1 < origName tycon2
284 pp_inst (InstInfo clas tvs ty theta _ _ _ _ _ _ _ _)
286 forall_ty = mkSigmaTy tvs theta (mkDictTy clas ty)
287 renumbered_ty = initNmbr (nmbrType forall_ty)
289 ppBesides [ppPStr SLIT("instance "), ppr_ty renumbered_ty, ppSemi]
292 %************************************************************************
294 \subsection{Printing tycons, classes, ...}
296 %************************************************************************
299 ppr_class :: Class -> Pretty
302 = --pprTrace "ppr_class:" (ppr PprDebug c) $
303 case (initNmbr (nmbrClass c)) of { -- renumber it!
304 Class _ n tyvar super_classes sdsels ops sels defms insts links ->
306 ppAbove (ppCat [ppPStr SLIT("class"), ppr_theta tyvar super_classes,
307 ppr_name n, ppr_tyvar tyvar,
308 if null ops then ppSemi else ppStr "where {"])
311 else ppAbove (ppNest 2 (ppAboves (map ppr_op ops)))
316 ppr_theta :: TyVar -> [Class] -> Pretty
318 ppr_theta tv [] = ppNil
319 ppr_theta tv [sc] = ppBeside (ppr_assert tv sc) (ppStr " =>")
320 ppr_theta tv super_classes
321 = ppBesides [ppLparen,
322 ppIntersperse pp'SP{-'-} (map (ppr_assert tv) super_classes),
325 ppr_assert tv (Class _ n _ _ _ _ _ _ _ _) = ppCat [ppr_name n, ppr_tyvar tv]
327 ppr_op (ClassOp o _ ty) = pp_sig (Unqual o) ty
331 ppr_val v ty -- renumber the type first!
332 = --pprTrace "ppr_val:" (ppr PprDebug v) $
333 pp_sig v (initNmbr (nmbrType ty))
336 = ppBesides [ppr_name op, ppPStr SLIT(" :: "), ppr_ty ty, ppSemi]
341 = --pprTrace "ppr_tycon:" (ppr PprDebug tycon) $
342 ppr_tc (initNmbr (nmbrTyCon tycon))
344 ------------------------
345 ppr_tc (PrimTyCon _ n _)
346 = ppCat [ ppStr "{- data", ppr_name n, ppStr " *built-in* -}" ]
349 = ppCat [ ppStr "{- data", ppr_name FunTyCon, ppStr " *built-in* -}" ]
351 ppr_tc (TupleTyCon _ n _)
352 = ppCat [ ppStr "{- ", ppr_name n, ppStr "-}" ]
354 ppr_tc (SynTyCon _ n _ _ tvs expand)
356 pp_tyvars = map ppr_tyvar tvs
358 ppBesides [ppPStr SLIT("type "), ppr_name n, ppSP, ppIntersperse ppSP pp_tyvars,
359 ppPStr SLIT(" = "), ppr_ty expand, ppSemi]
361 ppr_tc this_tycon@(DataTyCon u n k tvs ctxt cons derivings data_or_new)
362 = ppHang (ppCat [pp_data_or_new,
365 ppIntersperse ppSP (map ppr_tyvar tvs)])
367 (ppBeside pp_unabstract_condecls ppSemi)
368 -- NB: we do not print deriving info in interfaces
370 pp_data_or_new = case data_or_new of
371 DataType -> ppPStr SLIT("data")
372 NewType -> ppPStr SLIT("newtype")
374 ppr_context [] = ppNil
375 ppr_context [(c,t)] = ppCat [ppr_name c, ppr_ty t, ppStr "=>"]
377 = ppBesides[ppLparen,
378 ppInterleave ppComma [ppCat [ppr_name c, ppr_ty t] | (c,t) <- cs],
379 ppRparen, ppStr " =>"]
381 yes_we_print_condecls
382 = case (getExportFlag n) of
386 pp_unabstract_condecls
387 = if yes_we_print_condecls
388 then ppCat [ppEquals, pp_condecls]
395 ppSep ((ppr_con c) : (map ppr_next_con cs))
397 ppr_next_con con = ppCat [ppChar '|', ppr_con con]
401 (_, _, con_arg_tys, _) = dataConSig con
402 labels = dataConFieldLabels con -- none if not a record
403 strict_marks = dataConStrictMarks con
405 ppCat [ppr_unq_name con, ppr_fields labels strict_marks con_arg_tys]
407 ppr_fields labels strict_marks con_arg_tys
408 = if null labels then -- not a record thingy
409 ppIntersperse ppSP (zipWithEqual ppr_bang_ty strict_marks con_arg_tys)
412 ppInterleave ppComma (zipWith3Equal ppr_field labels strict_marks con_arg_tys),
416 = ppBeside (case b of { MarkedStrict -> ppChar '!'; _ -> ppNil })
417 (pprParendType PprInterface t)
420 = ppBesides [ppr_unq_name l, ppPStr SLIT(" :: "),
421 case b of { MarkedStrict -> ppChar '!'; _ -> ppNil },