summaryrefslogtreecommitdiff
path: root/ghc/compiler/main/MkIface.lhs
blob: d8ead0bcaa24d4c03741f85b918b9ec4a6610fcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
%
% (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
%
\section[MkIface]{Print an interface for a module}

\begin{code}
#include "HsVersions.h"

module MkIface (
	startIface, endIface,
	ifaceUsages,
	ifaceVersions,
	ifaceExportList,
	ifaceFixities,
	ifaceInstanceModules,
	ifaceDecls,
	ifaceInstances,
	ifacePragmas
    ) where

IMP_Ubiq(){-uitous-}
IMPORT_1_3(IO(Handle,hPutStr,openFile,hClose,IOMode(..)))

import Bag		( bagToList )
import Class		( GenClass(..){-instance NamedThing-}, GenClassOp(..) )
import CmdLineOpts	( opt_ProduceHi )
import FieldLabel	( FieldLabel{-instance NamedThing-} )
import FiniteMap	( emptyFM, addToFM, lookupFM, fmToList, eltsFM, FiniteMap )
import HsSyn
import Id		( idType, dataConRawArgTys, dataConFieldLabels, isDataCon,
			  dataConStrictMarks, StrictnessMark(..),
			  GenId{-instance NamedThing/Outputable-}
			)
import Maybes		( maybeToBool )
import Name		( origName, nameOf, moduleOf,
			  exportFlagOn, nameExportFlag, ExportFlag(..),
			  isLexSym, isLexCon, isLocallyDefined, isWiredInName,
			  RdrName(..){-instance Outputable-},
			  OrigName(..){-instance Ord-},
			  Name{-instance NamedThing-}
			)
import ParseUtils	( UsagesMap(..), VersionsMap(..) )
import PprEnv		-- not sure how much...
import PprStyle		( PprStyle(..) )
import PprType		-- most of it (??)
--import PrelMods	( modulesWithBuiltins )
import PrelInfo		( builtinValNamesMap, builtinTcNamesMap )
import Pretty		( prettyToUn )
import Unpretty		-- ditto
import RnHsSyn		( isRnConstr, SYN_IE(RenamedHsModule), RnName(..) )
import RnUtils		( SYN_IE(RnEnv) {-, pprRnEnv ToDo:rm-} )
import TcModule		( SYN_IE(TcIfaceInfo) )
import TcInstUtil	( InstInfo(..) )
import TyCon		( TyCon(..){-instance NamedThing-}, NewOrData(..) )
import Type		( mkSigmaTy, mkDictTy, getAppTyCon, splitForAllTy )
import Util		( sortLt, removeDups, zipWithEqual, zipWith3Equal, assertPanic, panic{-ToDo:rm-}{-, pprTrace ToDo:rm-} )

uppSemid   x = uppBeside (prettyToUn (ppr PprInterface x)) uppSemi -- micro util
ppr_ty	  ty = prettyToUn (pprType PprInterface ty)
ppr_tyvar tv = prettyToUn (ppr PprInterface tv)
ppr_name   n
  = case (origName "ppr_name" n) of { OrigName m s ->
    uppBesides [uppPStr m, uppChar '.', uppPStr s] }
\end{code}

We have a function @startIface@ to open the output file and put
(something like) ``interface Foo'' in it.  It gives back a handle
for subsequent additions to the interface file.

We then have one-function-per-block-of-interface-stuff, e.g.,
@ifaceExportList@ produces the @__exports__@ section; it appends
to the handle provided by @startIface@.

\begin{code}
startIface  :: Module
	    -> IO (Maybe Handle) -- Nothing <=> don't do an interface
endIface    :: Maybe Handle -> IO ()
ifaceUsages
	    :: Maybe Handle
	    -> UsagesMap
	    -> IO ()
ifaceVersions
	    :: Maybe Handle
	    -> VersionsMap
	    -> IO ()
ifaceExportList
	    :: Maybe Handle
	    -> (Name -> ExportFlag, ([(Name,ExportFlag)], [(Name,ExportFlag)]))
	    -> RnEnv
	    -> IO ()
ifaceFixities
	    :: Maybe Handle
	    -> RenamedHsModule
	    -> IO ()
ifaceInstanceModules
	    :: Maybe Handle
	    -> [Module]
	    -> IO ()
ifaceDecls  :: Maybe Handle
	    -> TcIfaceInfo  -- info produced by typechecker, for interfaces
	    -> IO ()
ifaceInstances
	    :: Maybe Handle
	    -> TcIfaceInfo  -- as above
	    -> IO ()
ifacePragmas
	    :: Maybe Handle
	    -> IO ()
ifacePragmas = panic "ifacePragmas" -- stub
\end{code}

\begin{code}
startIface mod
  = case opt_ProduceHi of
      Nothing -> return Nothing -- not producing any .hi file
      Just fn ->
	openFile fn WriteMode	>>= \ if_hdl ->
	hPutStr if_hdl ("{-# GHC_PRAGMA INTERFACE VERSION 20 #-}\ninterface "++ _UNPK_ mod) >>
	return (Just if_hdl)

endIface Nothing	= return ()
endIface (Just if_hdl)	= hPutStr if_hdl "\n" >> hClose if_hdl
\end{code}

\begin{code}
ifaceUsages Nothing{-no iface handle-} _ = return ()

ifaceUsages (Just if_hdl) usages
  | null usages_list
  = return ()
  | otherwise
  = hPutStr if_hdl "\n__usages__\n"   >>
    hPutStr if_hdl (uppShow 0 (uppAboves (map upp_uses usages_list)))
  where
    usages_list = fmToList usages -- NO: filter has_no_builtins (...)

--  has_no_builtins (m, _)
--    = m `notElem` modulesWithBuiltins
--    -- Don't *have* to do this; save gratuitous spillage in
--    -- every interface.  Could be flag-controlled...

    upp_uses (m, (mv, versions))
      = uppBesides [uppPStr m, uppSP, uppInt mv, uppPStr SLIT(" :: "),
	       upp_versions (fmToList versions), uppSemi]

    upp_versions nvs
      = uppIntersperse uppSP [ uppCat [uppPStr n, uppInt v] | (n,v) <- nvs ]
\end{code}

\begin{code}
ifaceVersions Nothing{-no iface handle-} _ = return ()

ifaceVersions (Just if_hdl) version_info
  | null version_list
  = return ()
  | otherwise
  = hPutStr if_hdl "\n__versions__\n"	>>
    hPutStr if_hdl (uppShow 0 (upp_versions version_list))
    -- NB: when compiling Prelude.hs, this will spew out
    -- stuff for [], (), (,), etc. [i.e., builtins], which
    -- we'd rather it didn't.  The version-mangling in
    -- the driver will ignore them.
  where
    version_list = fmToList version_info

    upp_versions nvs
      = uppAboves [ uppPStr n | (n,v) <- nvs ]
\end{code}

\begin{code}
ifaceInstanceModules Nothing{-no iface handle-} _ = return ()
ifaceInstanceModules (Just _)		       [] = return ()

ifaceInstanceModules (Just if_hdl) imods
  = hPutStr if_hdl "\n__instance_modules__\n" >>
    hPutStr if_hdl (uppShow 0 (uppCat (map uppPStr imods)))
\end{code}

Export list: grab the Names of things that are marked Exported, sort
(so the interface file doesn't ``wobble'' from one compilation to the
next...), and print.  We work from the renamer's final ``RnEnv'',
which has all the names we might possibly be interested in.
(Note that the ``module X'' export items can cause a lot of grief.)
\begin{code}
ifaceExportList Nothing{-no iface handle-} _ _ = return ()

ifaceExportList (Just if_hdl)
		(export_fn, (dotdot_vals, dotdot_tcs))
		rn_env@((qual, unqual, tc_qual, tc_unqual), _)
  = let
	name_flag_pairs :: FiniteMap OrigName ExportFlag
	name_flag_pairs
	  = foldr (from_wired  True{-val-ish-})
	   (foldr (from_wired  False{-tycon-ish-})
	   (foldr (from_dotdot True{-val-ish-})
	   (foldr (from_dotdot False{-tycon-ish-})
	   (foldr from_val
	   (foldr from_val
	   (foldr from_tc
	   (foldr from_tc emptyFM{-init accum-}
		  (eltsFM tc_unqual))
		  (eltsFM tc_qual))
		  (eltsFM unqual))
		  (eltsFM qual))
		  dotdot_tcs)
		  dotdot_vals)
		  (eltsFM builtinTcNamesMap))
		  (eltsFM builtinValNamesMap)

	sorted_pairs = sortLt lexical_lt (fmToList name_flag_pairs)

    in
    --pprTrace "Exporting:" (pprRnEnv PprDebug rn_env) $
    hPutStr if_hdl "\n__exports__\n" >>
    hPutStr if_hdl (uppShow 0 (uppAboves (map upp_pair sorted_pairs)))
  where
    from_val rn acc
      | fun_looking rn && exportFlagOn ef = addToFM acc on ef
      | otherwise			  = acc
      where
	ef = export_fn n -- NB: using the export fn!
	n  = getName rn
	on = origName "from_val" n

    -- fun_looking: must avoid class ops and data constructors
    -- and record fieldnames
    fun_looking (RnName    _) = True
    fun_looking (WiredInId i) = not (isDataCon i)
    fun_looking _		  = False

    from_tc rn acc
      | exportFlagOn ef = addToFM acc on ef
      | otherwise	= acc
      where
	ef = export_fn n -- NB: using the export fn!
	n  = getName rn
	on = origName "from_tc" n

    from_dotdot is_valish (n,ef) acc
      | is_valish && isLexCon str = acc
      | exportFlagOn ef		  = addToFM acc on ef
      | otherwise		  = acc
      where
	on = origName "from_dotdot" n
	(OrigName _ str) = on

    from_wired is_val_ish rn acc
      | is_val_ish && not (fun_looking rn)
			= acc -- these things don't cause export-ery
      | exportFlagOn ef = addToFM acc on ef
      | otherwise       = acc
      where
	n  = getName rn
	ef = export_fn n
	on = origName "from_wired" n

    --------------
    lexical_lt (n1,_) (n2,_) = n1 < n2

    --------------
    upp_pair (OrigName m n, ef)
      = uppBesides [uppPStr m, uppSP, uppPStr n, uppSP, upp_export ef]
      where
	upp_export ExportAll = uppPStr SLIT("(..)")
	upp_export ExportAbs = uppNil
\end{code}

\begin{code}
ifaceFixities Nothing{-no iface handle-} _ = return ()

ifaceFixities (Just if_hdl) (HsModule _ _ _ _ fixities _ _ _ _ _ _ _ _ _)
  = let
	pp_fixities = foldr go [] fixities
    in
    if null pp_fixities then
	return ()
    else 
	hPutStr if_hdl "\n__fixities__\n" >>
	hPutStr if_hdl (uppShow 0 (uppAboves pp_fixities))
  where
    go (InfixL v i) acc = (if isLocallyDefined v then (:) (print_fix "l" i v) else id) acc
    go (InfixR v i) acc = (if isLocallyDefined v then (:) (print_fix "r" i v) else id) acc
    go (InfixN v i) acc = (if isLocallyDefined v then (:) (print_fix ""  i v) else id) acc

    print_fix suff prec var
      = uppBesides [uppPStr SLIT("infix"), uppStr suff, uppSP, uppInt prec, uppSP, ppr_name var, uppSemi]
\end{code}

\begin{code}
non_wired x = not (isWiredInName (getName x)) --ToDo:move?

ifaceDecls Nothing{-no iface handle-} _ = return ()

ifaceDecls (Just if_hdl) (vals, tycons, classes, _)
  = ASSERT(all isLocallyDefined vals)
    ASSERT(all isLocallyDefined tycons)
    ASSERT(all isLocallyDefined classes)
    let
	nonwired_classes = filter non_wired classes
	nonwired_tycons  = filter non_wired tycons
	nonwired_vals    = filter non_wired vals

	lt_lexical a b = origName "lt_lexical" a < origName "lt_lexical" b

	sorted_classes = sortLt lt_lexical nonwired_classes
	sorted_tycons  = sortLt lt_lexical nonwired_tycons
	sorted_vals    = sortLt lt_lexical nonwired_vals
    in
    if (null sorted_classes && null sorted_tycons && null sorted_vals) then
	--  You could have a module with just (re-)exports/instances in it
	return ()
    else
    hPutStr if_hdl "\n__declarations__\n" >>
    hPutStr if_hdl (uppShow 0 (uppAboves [
	uppAboves (map ppr_class sorted_classes),
	uppAboves (map ppr_tycon sorted_tycons),
	uppAboves [ppr_val v (idType v) | v <- sorted_vals]]))
\end{code}

\begin{code}
ifaceInstances Nothing{-no iface handle-} _ = return ()

ifaceInstances (Just if_hdl) (_, _, _, insts)
  = let
	togo_insts	= filter is_togo_inst (bagToList insts)

	sorted_insts	= sortLt lt_inst togo_insts
    in
    if null togo_insts then
	return ()
    else
	hPutStr if_hdl "\n__instances__\n" >>
	hPutStr if_hdl (uppShow 0 (uppAboves (map pp_inst sorted_insts)))
  where
    is_togo_inst (InstInfo clas _ ty _ _ _ _ _ from_here _ _ _)
      = from_here -- && ...

    -------
    lt_inst (InstInfo clas1 _ ty1 _ _ _ _ _ _ _ _ _)
	    (InstInfo clas2 _ ty2 _ _ _ _ _ _ _ _ _)
      = let
	    tycon1 = fst (getAppTyCon ty1)
	    tycon2 = fst (getAppTyCon ty2)
	in
	case (origName "lt_inst" clas1 `cmp` origName "lt_inst" clas2) of
	  LT_ -> True
	  GT_ -> False
	  EQ_ -> origName "lt_inst2" tycon1 < origName "lt_inst2" tycon2

    -------
    pp_inst (InstInfo clas tvs ty theta _ _ _ _ _ _ _ _)
      = let
	    forall_ty     = mkSigmaTy tvs theta (mkDictTy clas ty)
	    renumbered_ty = initNmbr (nmbrType forall_ty)
	in
	case (splitForAllTy renumbered_ty) of { (rtvs, rrho_ty) ->
	uppBesides [uppPStr SLIT("instance "), ppr_forall rtvs, ppr_ty rrho_ty, uppSemi] }
\end{code}

%************************************************************************
%*									*
\subsection{Printing tycons, classes, ...}
%*									*
%************************************************************************

\begin{code}
ppr_class :: Class -> Unpretty

ppr_class c
  = --pprTrace "ppr_class:" (ppr PprDebug c) $
    case (initNmbr (nmbrClass c)) of { -- renumber it!
      Class _ n tyvar super_classes sdsels ops sels defms insts links ->

	uppCat [uppPStr SLIT("class"), ppr_context tyvar super_classes,
		ppr_name n, ppr_tyvar tyvar,
		if null ops
		then uppSemi
		else uppCat [uppStr "where {", uppCat (map ppr_op ops), uppStr "};"]]
    }
  where
    ppr_context :: TyVar -> [Class] -> Unpretty

    ppr_context tv []   = uppNil
--  ppr_context tv [sc] = uppBeside (ppr_assert tv sc) (uppPStr SLIT(" =>"))
    ppr_context tv super_classes
      = uppBesides [uppStr "{{",
		    uppIntersperse upp'SP{-'-} (map (ppr_assert tv) super_classes),
		    uppStr "}} =>"]

    ppr_assert tv (Class _ n _ _ _ _ _ _ _ _) = uppCat [ppr_name n, ppr_tyvar tv]

    clas_mod = moduleOf (origName "ppr_class" c)

    ppr_op (ClassOp o _ ty) = pp_sig (Qual clas_mod o) ty
\end{code}

\begin{code}
ppr_val v ty -- renumber the type first!
  = --pprTrace "ppr_val:" (ppr PprDebug v) $
    pp_sig v (initNmbr (nmbrType ty))

pp_sig op ty
  = case (splitForAllTy ty) of { (tvs, rho_ty) ->
    uppBesides [ppr_name op, uppPStr SLIT(" :: "), ppr_forall tvs, ppr_ty rho_ty, uppSemi] }

ppr_forall []  = uppNil
ppr_forall tvs = uppBesides [ uppStr "__forall__ [", uppInterleave uppComma (map ppr_tyvar tvs), uppStr "] " ]
\end{code}

\begin{code}
ppr_tycon tycon
  = --pprTrace "ppr_tycon:" (ppr PprDebug tycon) $
    ppr_tc (initNmbr (nmbrTyCon tycon))

------------------------
ppr_tc (PrimTyCon _ n _ _)
  = uppCat [ uppStr "{- data", ppr_name n, uppStr " *built-in* -}" ]

ppr_tc FunTyCon
  = uppCat [ uppStr "{- data", ppr_name FunTyCon, uppStr " *built-in* -}" ]

ppr_tc (TupleTyCon _ n _)
  = uppCat [ uppStr "{- ", ppr_name n, uppStr "-}" ]

ppr_tc (SynTyCon _ n _ _ tvs expand)
  = let
	pp_tyvars   = map ppr_tyvar tvs
    in
    uppBesides [uppPStr SLIT("type "), ppr_name n, uppSP, uppIntersperse uppSP pp_tyvars,
	   uppPStr SLIT(" = "), ppr_ty expand, uppSemi]

ppr_tc this_tycon@(DataTyCon u n k tvs ctxt cons derivings data_or_new)
  = uppCat [pp_data_or_new,
	   ppr_context ctxt,
	   ppr_name n,
	   uppIntersperse uppSP (map ppr_tyvar tvs),
	   uppEquals, pp_condecls,
	   uppSemi]
	   -- NB: we do not print deriving info in interfaces
  where
    pp_data_or_new = case data_or_new of
		      DataType -> uppPStr SLIT("data")
		      NewType  -> uppPStr SLIT("newtype")

    ppr_context []      = uppNil
--  ppr_context [(c,t)] = uppCat [ppr_name c, ppr_ty t, uppPStr SLIT("=>")]
    ppr_context cs
      = uppBesides[uppStr "{{",
		   uppInterleave uppComma [uppCat [ppr_name c, ppr_ty t] | (c,t) <- cs],
		   uppStr "}}", uppPStr SLIT(" =>")]

    pp_condecls
      = let
	    (c:cs) = cons
	in
	uppCat ((ppr_con c) : (map ppr_next_con cs))

    ppr_next_con con = uppCat [uppChar '|', ppr_con con]

    ppr_con con
      = let
	    con_arg_tys  = dataConRawArgTys   con
	    labels       = dataConFieldLabels con -- none if not a record
	    strict_marks = dataConStrictMarks con
	in
	uppCat [ppr_name con, ppr_fields labels strict_marks con_arg_tys]

    ppr_fields labels strict_marks con_arg_tys
      = if null labels then -- not a record thingy
	    uppIntersperse uppSP (zipWithEqual  "ppr_fields" ppr_bang_ty strict_marks con_arg_tys)
	else
	    uppCat [ uppChar '{',
	    uppInterleave uppComma (zipWith3Equal "ppr_field" ppr_field labels strict_marks con_arg_tys),
	    uppChar '}' ]

    ppr_bang_ty b t
      = uppBeside (case b of { MarkedStrict -> uppChar '!'; _ -> uppNil })
		  (prettyToUn (pprParendType PprInterface t))

    ppr_field l b t
      = uppBesides [ppr_name l, uppPStr SLIT(" :: "),
		   case b of { MarkedStrict -> uppChar '!'; _ -> uppNil },
		   ppr_ty t]
\end{code}