[project @ 2000-11-20 16:07:12 by simonpj]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsDecls.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[HsDecls]{Abstract syntax: global declarations}
5
6 Definitions for: @TyDecl@ and @ConDecl@, @ClassDecl@,
7 @InstDecl@, @DefaultDecl@ and @ForeignDecl@.
8
9 \begin{code}
10 module HsDecls (
11         HsDecl(..), TyClDecl(..), InstDecl(..), RuleDecl(..), RuleBndr(..),
12         DefaultDecl(..), ForeignDecl(..), ForKind(..),
13         ExtName(..), isDynamicExtName, extNameStatic,
14         ConDecl(..), ConDetails(..), 
15         BangType(..), getBangType,
16         DeprecDecl(..), DeprecTxt,
17         hsDeclName, instDeclName, tyClDeclName, tyClDeclNames, tyClDeclSysNames,
18         isClassDecl, isSynDecl, isDataDecl, isIfaceSigDecl, countTyClDecls,
19         mkClassDeclSysNames, isIfaceRuleDecl, ifaceRuleDeclName,
20         getClassDeclSysNames, conDetailsTys
21     ) where
22
23 #include "HsVersions.h"
24
25 -- friends:
26 import HsBinds          ( HsBinds, MonoBinds, Sig(..), FixitySig(..) )
27 import HsExpr           ( HsExpr )
28 import HsTypes
29 import PprCore          ( pprCoreRule )
30 import HsCore           ( UfExpr, UfBinder, HsIdInfo, pprHsIdInfo,
31                           eq_ufBinders, eq_ufExpr, pprUfExpr 
32                         )
33 import CoreSyn          ( CoreRule(..) )
34 import BasicTypes       ( NewOrData(..) )
35 import CallConv         ( CallConv, pprCallConv )
36
37 -- others:
38 import Name             ( NamedThing )
39 import FunDeps          ( pprFundeps )
40 import Class            ( FunDep, DefMeth(..) )
41 import CStrings         ( CLabelString, pprCLabelString )
42 import Outputable       
43 import SrcLoc           ( SrcLoc )
44 \end{code}
45
46
47 %************************************************************************
48 %*                                                                      *
49 \subsection[HsDecl]{Declarations}
50 %*                                                                      *
51 %************************************************************************
52
53 \begin{code}
54 data HsDecl name pat
55   = TyClD       (TyClDecl name pat)
56   | InstD       (InstDecl  name pat)
57   | DefD        (DefaultDecl name)
58   | ValD        (HsBinds name pat)
59   | ForD        (ForeignDecl name)
60   | FixD        (FixitySig name)
61   | DeprecD     (DeprecDecl name)
62   | RuleD       (RuleDecl name pat)
63
64 -- NB: all top-level fixity decls are contained EITHER
65 -- EITHER FixDs
66 -- OR     in the ClassDecls in TyClDs
67 --
68 -- The former covers
69 --      a) data constructors
70 --      b) class methods (but they can be also done in the
71 --              signatures of class decls)
72 --      c) imported functions (that have an IfacSig)
73 --      d) top level decls
74 --
75 -- The latter is for class methods only
76 \end{code}
77
78 \begin{code}
79 #ifdef DEBUG
80 hsDeclName :: (NamedThing name, Outputable name, Outputable pat)
81            => HsDecl name pat -> name
82 #endif
83 hsDeclName (TyClD decl)                             = tyClDeclName decl
84 hsDeclName (InstD   decl)                           = instDeclName decl
85 hsDeclName (ForD    (ForeignDecl name _ _ _ _ _))   = name
86 hsDeclName (FixD    (FixitySig name _ _))           = name
87 -- Others don't make sense
88 #ifdef DEBUG
89 hsDeclName x                                  = pprPanic "HsDecls.hsDeclName" (ppr x)
90 #endif
91
92
93 instDeclName :: InstDecl name pat -> name
94 instDeclName (InstDecl _ _ _ (Just name) _) = name
95
96 \end{code}
97
98 \begin{code}
99 instance (NamedThing name, Outputable name, Outputable pat)
100         => Outputable (HsDecl name pat) where
101
102     ppr (TyClD dcl)  = ppr dcl
103     ppr (ValD binds) = ppr binds
104     ppr (DefD def)   = ppr def
105     ppr (InstD inst) = ppr inst
106     ppr (ForD fd)    = ppr fd
107     ppr (FixD fd)    = ppr fd
108     ppr (RuleD rd)   = ppr rd
109     ppr (DeprecD dd) = ppr dd
110 \end{code}
111
112
113 %************************************************************************
114 %*                                                                      *
115 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
116 %*                                                                      *
117 %************************************************************************
118
119 Type and class declarations carry 'implicit names'.  In particular:
120
121 Type A.  
122 ~~~~~~~
123   Each data type decl defines 
124         a worker name for each constructor
125         to-T and from-T convertors
126   Each class decl defines
127         a tycon for the class
128         a data constructor for that tycon
129         the worker for that constructor
130         a selector for each superclass
131
132 All have occurrence names that are derived uniquely from their parent declaration.
133
134 None of these get separate definitions in an interface file; they are
135 fully defined by the data or class decl.  But they may *occur* in
136 interface files, of course.  Any such occurrence must haul in the
137 relevant type or class decl.
138
139 Plan of attack:
140  - Make up their occurrence names immediately
141
142  - Ensure they "point to" the parent data/class decl 
143    when loading that decl from an interface file
144
145  - When renaming the decl look them up in the name cache,
146    ensure correct module and provenance is set
147
148 Type B: Default methods and dictionary functions
149 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150 Have their own binding in an interface file.
151
152 Default methods : occurrence name is derived uniquely from the class decl.
153 Dict functions  : occurrence name is derived from the instance decl, plus a unique number.
154
155 Plan of attack: 
156   - Do *not* make them point to the parent class decl
157   - Interface-file decls: treat just like Type A
158   - Source-file decls:    the names aren't in the decl at all; 
159                           instead the typechecker makes them up
160
161 \begin{code}
162 data TyClDecl name pat
163   = IfaceSig    name                    -- It may seem odd to classify an interface-file signature
164                 (HsType name)           -- as a 'TyClDecl', but it's very convenient.  These three
165                 [HsIdInfo name]         -- are the kind that appear in interface files.
166                 SrcLoc
167
168   | TyData      NewOrData
169                 (HsContext name) -- context
170                 name             -- type constructor
171                 [HsTyVarBndr name]       -- type variables
172                 [ConDecl name]   -- data constructors (empty if abstract)
173                 Int              -- Number of data constructors (valid even if type is abstract)
174                 (Maybe [name])   -- derivings; Nothing => not specified
175                                  -- (i.e., derive default); Just [] => derive
176                                  -- *nothing*; Just <list> => as you would
177                                  -- expect...
178                 SrcLoc
179                 name             -- generic converter functions
180                 name             -- generic converter functions
181
182   | TySynonym   name                    -- type constructor
183                 [HsTyVarBndr name]      -- type variables
184                 (HsType name)           -- synonym expansion
185                 SrcLoc
186
187   | ClassDecl   (HsContext name)        -- context...
188                 name                    -- name of the class
189                 [HsTyVarBndr name]      -- the class type variables
190                 [FunDep name]           -- functional dependencies
191                 [Sig name]              -- methods' signatures
192                 (MonoBinds name pat)    -- default methods
193                 (ClassDeclSysNames name)
194                 SrcLoc
195 \end{code}
196
197 Simple classifiers
198
199 \begin{code}
200 isIfaceSigDecl, isDataDecl, isSynDecl, isClassDecl :: TyClDecl name pat -> Bool
201
202 isIfaceSigDecl (IfaceSig _ _ _ _) = True
203 isIfaceSigDecl other              = False
204
205 isSynDecl (TySynonym _ _ _ _) = True
206 isSynDecl other               = False
207
208 isDataDecl (TyData _ _ _ _ _ _ _ _ _ _) = True
209 isDataDecl other                        = False
210
211 isClassDecl (ClassDecl _ _ _ _ _ _ _ _ ) = True
212 isClassDecl other                        = False
213 \end{code}
214
215 Dealing with names
216
217 \begin{code}
218 --------------------------------
219 tyClDeclName :: TyClDecl name pat -> name
220 tyClDeclName (IfaceSig name _ _ _)           = name
221 tyClDeclName (TyData _ _ name _ _ _ _ _ _ _) = name
222 tyClDeclName (TySynonym name _ _ _)          = name
223 tyClDeclName (ClassDecl _ name _ _ _ _ _ _)  = name
224
225
226 --------------------------------
227 tyClDeclNames :: Eq name => TyClDecl name pat -> [(name, SrcLoc)]
228 -- Returns all the binding names of the decl, along with their SrcLocs
229 -- The first one is guaranteed to be the name of the decl
230 -- For record fields, the first one counts as the SrcLoc
231 -- We use the equality to filter out duplicate field names
232
233 tyClDeclNames (TySynonym name _ _ loc)
234   = [(name,loc)]
235
236 tyClDeclNames (ClassDecl _ cls_name _ _ sigs _ _ loc)
237   = (cls_name,loc) : [(n,loc) | ClassOpSig n _ _ loc <- sigs]
238
239 tyClDeclNames (TyData _ _ tc_name _ cons _ _ loc _ _)
240   = (tc_name,loc) : conDeclsNames cons
241
242 tyClDeclNames (IfaceSig name _ _ loc) = [(name,loc)]
243
244 --------------------------------
245 tyClDeclSysNames :: TyClDecl name pat -> [(name, SrcLoc)]
246 -- Similar to tyClDeclNames, but returns the "implicit" 
247 -- or "system" names of the declaration
248
249 tyClDeclSysNames (ClassDecl _ _ _ _ _ _ names loc) = [(n,loc)        | n <- names]
250 tyClDeclSysNames (TyData _ _ _ _ cons _ _ _ _ _)   = [(wkr_name,loc) | ConDecl _ wkr_name _ _ _ loc <- cons]
251 tyClDeclSysNames decl                              = []
252
253
254 --------------------------------
255 type ClassDeclSysNames name = [name]
256         --      [tycon, datacon wrapper, datacon worker, 
257         --       superclass selector 1, ..., superclass selector n]
258         -- They are kept in a list rather than a tuple to make the
259         -- renamer easier.
260
261 mkClassDeclSysNames  :: (name, name, name, [name]) -> [name]
262 getClassDeclSysNames :: [name] -> (name, name, name, [name])
263 mkClassDeclSysNames  (a,b,c,ds) = a:b:c:ds
264 getClassDeclSysNames (a:b:c:ds) = (a,b,c,ds)
265 \end{code}
266
267 \begin{code}
268 instance (NamedThing name, Ord name) => Eq (TyClDecl name pat) where
269         -- Used only when building interface files
270   (==) (IfaceSig n1 t1 i1 _)
271        (IfaceSig n2 t2 i2 _) = n1==n2 && t1==t2 && i1==i2
272
273   (==) (TyData nd1 cxt1 n1 tvs1 cons1 _ _ _ _ _)
274        (TyData nd2 cxt2 n2 tvs2 cons2 _ _ _ _ _)
275     = n1 == n2 &&
276       nd1 == nd2 &&
277       eqWithHsTyVars tvs1 tvs2 (\ env -> 
278           eq_hsContext env cxt1 cxt2  &&
279           eqListBy (eq_ConDecl env) cons1 cons2
280       )
281
282   (==) (TySynonym n1 tvs1 ty1 _)
283        (TySynonym n2 tvs2 ty2 _)
284     =  n1 == n2 &&
285        eqWithHsTyVars tvs1 tvs2 (\ env -> eq_hsType env ty1 ty2)
286
287   (==) (ClassDecl cxt1 n1 tvs1 fds1 sigs1 _ _ _ )
288        (ClassDecl cxt2 n2 tvs2 fds2 sigs2 _ _ _ )
289     =  n1 == n2 &&
290        eqWithHsTyVars tvs1 tvs2 (\ env -> 
291           eq_hsContext env cxt1 cxt2 &&
292           eqListBy (eq_hsFD env) fds1 fds2 &&
293           eqListBy (eq_cls_sig env) sigs1 sigs2
294        )
295
296   (==) _ _ = False      -- default case
297
298 eq_hsFD env (ns1,ms1) (ns2,ms2)
299   = eqListBy (eq_hsVar env) ns1 ns2 && eqListBy (eq_hsVar env) ms1 ms2
300
301 eq_cls_sig env (ClassOpSig n1 dm1 ty1 _) (ClassOpSig n2 dm2 ty2 _)
302   = n1==n2 && dm1 `eq_dm` dm2 && eq_hsType env ty1 ty2
303   where
304         -- Ignore the name of the default method for (DefMeth id)
305         -- This is used for comparing declarations before putting
306         -- them into interface files, and the name of the default 
307         -- method isn't relevant
308     Nothing            `eq_dm` Nothing            = True
309     (Just NoDefMeth)   `eq_dm` (Just NoDefMeth)   = True
310     (Just GenDefMeth)  `eq_dm` (Just GenDefMeth)  = True
311     (Just (DefMeth _)) `eq_dm` (Just (DefMeth _)) = True
312     dm1                `eq_dm` dm2                = False
313
314     
315 \end{code}
316
317 \begin{code}
318 countTyClDecls :: [TyClDecl name pat] -> (Int, Int, Int, Int, Int)
319         -- class, data, newtype, synonym decls
320 countTyClDecls decls 
321  = (length [() | ClassDecl _ _ _ _ _ _ _ _         <- decls],
322     length [() | TyData DataType _ _ _ _ _ _ _ _ _ <- decls],
323     length [() | TyData NewType  _ _ _ _ _ _ _ _ _ <- decls],
324     length [() | TySynonym _ _ _ _                 <- decls],
325     length [() | IfaceSig _ _ _ _                  <- decls])
326 \end{code}
327
328 \begin{code}
329 instance (NamedThing name, Outputable name, Outputable pat)
330               => Outputable (TyClDecl name pat) where
331
332     ppr (IfaceSig var ty info _) = hsep [ppr var, dcolon, ppr ty, pprHsIdInfo info]
333
334     ppr (TySynonym tycon tyvars mono_ty src_loc)
335       = hang (ptext SLIT("type") <+> pp_decl_head [] tycon tyvars <+> equals)
336              4 (ppr mono_ty)
337
338     ppr (TyData new_or_data context tycon tyvars condecls ncons 
339                 derivings src_loc gen_conv1 gen_conv2) -- The generic names are not printed out ATM
340       = pp_tydecl
341                   (ptext keyword <+> pp_decl_head context tycon tyvars <+> equals)
342                   (pp_condecls condecls ncons)
343                   derivings
344       where
345         keyword = case new_or_data of
346                         NewType  -> SLIT("newtype")
347                         DataType -> SLIT("data")
348
349     ppr (ClassDecl context clas tyvars fds sigs methods _ src_loc)
350       | null sigs       -- No "where" part
351       = top_matter
352
353       | otherwise       -- Laid out
354       = sep [hsep [top_matter, ptext SLIT("where {")],
355              nest 4 (sep [sep (map ppr_sig sigs), pp_methods, char '}'])]
356       where
357         top_matter  = ptext SLIT("class") <+> pp_decl_head context clas tyvars <+> pprFundeps fds
358         ppr_sig sig = ppr sig <> semi
359         pp_methods = getPprStyle $ \ sty ->
360                      if ifaceStyle sty then empty else ppr methods
361         
362 pp_decl_head :: Outputable name => HsContext name -> name -> [HsTyVarBndr name] -> SDoc
363 pp_decl_head context thing tyvars = hsep [pprHsContext context, ppr thing, interppSP tyvars]
364
365 pp_condecls []     ncons = ptext SLIT("{- abstract with") <+> int ncons <+> ptext SLIT("constructors -}")
366 pp_condecls (c:cs) ncons = sep (ppr c : map (\ c -> ptext SLIT("|") <+> ppr c) cs)
367
368 pp_tydecl pp_head pp_decl_rhs derivings
369   = hang pp_head 4 (sep [
370         pp_decl_rhs,
371         case derivings of
372           Nothing          -> empty
373           Just ds          -> hsep [ptext SLIT("deriving"), parens (interpp'SP ds)]
374     ])
375 \end{code}
376
377
378 %************************************************************************
379 %*                                                                      *
380 \subsection[ConDecl]{A data-constructor declaration}
381 %*                                                                      *
382 %************************************************************************
383
384 \begin{code}
385 data ConDecl name
386   = ConDecl     name                    -- Constructor name; this is used for the
387                                         -- DataCon itself, and for the user-callable wrapper Id
388
389                 name                    -- Name of the constructor's 'worker Id'
390                                         -- Filled in as the ConDecl is built
391
392                 [HsTyVarBndr name]      -- Existentially quantified type variables
393                 (HsContext name)        -- ...and context
394                                         -- If both are empty then there are no existentials
395
396                 (ConDetails name)
397                 SrcLoc
398
399 data ConDetails name
400   = VanillaCon                  -- prefix-style con decl
401                 [BangType name]
402
403   | InfixCon                    -- infix-style con decl
404                 (BangType name)
405                 (BangType name)
406
407   | RecCon                      -- record-style con decl
408                 [([name], BangType name)]       -- list of "fields"
409 \end{code}
410
411 \begin{code}
412 conDeclsNames :: Eq name => [ConDecl name] -> [(name,SrcLoc)]
413   -- See tyClDeclNames for what this does
414   -- The function is boringly complicated because of the records
415   -- And since we only have equality, we have to be a little careful
416 conDeclsNames cons
417   = snd (foldl do_one ([], []) cons)
418   where
419     do_one (flds_seen, acc) (ConDecl name _ _ _ details loc)
420         = do_details ((name,loc):acc) details
421         where
422           do_details acc (RecCon flds) = foldl do_fld (flds_seen, acc) flds
423           do_details acc other         = (flds_seen, acc)
424
425           do_fld acc (flds, _) = foldl do_fld1 acc flds
426
427           do_fld1 (flds_seen, acc) fld
428                 | fld `elem` flds_seen = (flds_seen,acc)
429                 | otherwise            = (fld:flds_seen, (fld,loc):acc)
430 \end{code}
431
432 \begin{code}
433 conDetailsTys :: ConDetails name -> [HsType name]
434 conDetailsTys (VanillaCon btys)    = map getBangType btys
435 conDetailsTys (InfixCon bty1 bty2) = [getBangType bty1, getBangType bty2]
436 conDetailsTys (RecCon fields)      = [getBangType bty | (_, bty) <- fields]
437
438
439 eq_ConDecl env (ConDecl n1 _ tvs1 cxt1 cds1 _)
440                (ConDecl n2 _ tvs2 cxt2 cds2 _)
441   = n1 == n2 &&
442     (eq_hsTyVars env tvs1 tvs2  $ \ env ->
443      eq_hsContext env cxt1 cxt2 &&
444      eq_ConDetails env cds1 cds2)
445
446 eq_ConDetails env (VanillaCon bts1) (VanillaCon bts2)
447   = eqListBy (eq_btype env) bts1 bts2
448 eq_ConDetails env (InfixCon bta1 btb1) (InfixCon bta2 btb2)
449   = eq_btype env bta1 bta2 && eq_btype env btb1 btb2
450 eq_ConDetails env (RecCon fs1) (RecCon fs2)
451   = eqListBy (eq_fld env) fs1 fs2
452 eq_ConDetails env _ _ = False
453
454 eq_fld env (ns1,bt1) (ns2, bt2) = ns1==ns2 && eq_btype env bt1 bt2
455 \end{code}
456   
457 \begin{code}
458 data BangType name
459   = Banged   (HsType name)      -- HsType: to allow Haskell extensions
460   | Unbanged (HsType name)      -- (MonoType only needed for straight Haskell)
461   | Unpacked (HsType name)      -- Field is strict and to be unpacked if poss.
462
463 getBangType (Banged ty)   = ty
464 getBangType (Unbanged ty) = ty
465 getBangType (Unpacked ty) = ty
466
467 eq_btype env (Banged t1)   (Banged t2)   = eq_hsType env t1 t2
468 eq_btype env (Unbanged t1) (Unbanged t2) = eq_hsType env t1 t2
469 eq_btype env (Unpacked t1) (Unpacked t2) = eq_hsType env t1 t2
470 eq_btype env _             _             = False
471 \end{code}
472
473 \begin{code}
474 instance (Outputable name) => Outputable (ConDecl name) where
475     ppr (ConDecl con _ tvs cxt con_details  loc)
476       = sep [pprHsForAll tvs cxt, ppr_con_details con con_details]
477
478 ppr_con_details con (InfixCon ty1 ty2)
479   = hsep [ppr_bang ty1, ppr con, ppr_bang ty2]
480
481 ppr_con_details con (VanillaCon tys)
482   = ppr con <+> hsep (map (ppr_bang) tys)
483
484 ppr_con_details con (RecCon fields)
485   = ppr con <+> braces (hsep (punctuate comma (map ppr_field fields)))
486   where
487     ppr_field (ns, ty) = hsep (map (ppr) ns) <+> 
488                          dcolon <+>
489                          ppr_bang ty
490
491 instance Outputable name => Outputable (BangType name) where
492     ppr = ppr_bang
493
494 ppr_bang (Banged   ty) = ptext SLIT("!") <> pprParendHsType ty
495 ppr_bang (Unbanged ty) = pprParendHsType ty
496 ppr_bang (Unpacked ty) = ptext SLIT("! !") <> pprParendHsType ty
497 \end{code}
498
499
500 %************************************************************************
501 %*                                                                      *
502 \subsection[InstDecl]{An instance declaration
503 %*                                                                      *
504 %************************************************************************
505
506 \begin{code}
507 data InstDecl name pat
508   = InstDecl    (HsType name)   -- Context => Class Instance-type
509                                 -- Using a polytype means that the renamer conveniently
510                                 -- figures out the quantified type variables for us.
511
512                 (MonoBinds name pat)
513
514                 [Sig name]              -- User-supplied pragmatic info
515
516                 (Maybe name)            -- Name for the dictionary function
517                                         -- Nothing for source-file instance decls
518
519                 SrcLoc
520 \end{code}
521
522 \begin{code}
523 instance (Outputable name, Outputable pat)
524               => Outputable (InstDecl name pat) where
525
526     ppr (InstDecl inst_ty binds uprags maybe_dfun_name src_loc)
527       = getPprStyle $ \ sty ->
528         if ifaceStyle sty then
529            hsep [ptext SLIT("instance"), ppr inst_ty, equals, pp_dfun]
530         else
531            vcat [hsep [ptext SLIT("instance"), ppr inst_ty, ptext SLIT("where")],
532                  nest 4 (ppr uprags),
533                  nest 4 (ppr binds) ]
534       where
535         pp_dfun = case maybe_dfun_name of
536                     Just df -> ppr df
537                     Nothing -> empty
538 \end{code}
539
540 \begin{code}
541 instance Ord name => Eq (InstDecl name pat) where
542         -- Used for interface comparison only, so don't compare bindings
543   (==) (InstDecl inst_ty1 _ _ dfun1 _) (InstDecl inst_ty2 _ _ dfun2 _)
544        = inst_ty1 == inst_ty2 && dfun1 == dfun2
545 \end{code}
546
547
548 %************************************************************************
549 %*                                                                      *
550 \subsection[DefaultDecl]{A @default@ declaration}
551 %*                                                                      *
552 %************************************************************************
553
554 There can only be one default declaration per module, but it is hard
555 for the parser to check that; we pass them all through in the abstract
556 syntax, and that restriction must be checked in the front end.
557
558 \begin{code}
559 data DefaultDecl name
560   = DefaultDecl [HsType name]
561                 SrcLoc
562
563 instance (Outputable name)
564               => Outputable (DefaultDecl name) where
565
566     ppr (DefaultDecl tys src_loc)
567       = ptext SLIT("default") <+> parens (interpp'SP tys)
568 \end{code}
569
570 %************************************************************************
571 %*                                                                      *
572 \subsection{Foreign function interface declaration}
573 %*                                                                      *
574 %************************************************************************
575
576 \begin{code}
577 data ForeignDecl name = 
578    ForeignDecl 
579         name 
580         ForKind   
581         (HsType name)
582         ExtName
583         CallConv
584         SrcLoc
585
586 instance (Outputable name)
587               => Outputable (ForeignDecl name) where
588
589     ppr (ForeignDecl nm imp_exp ty ext_name cconv src_loc)
590       = ptext SLIT("foreign") <+> ppr_imp_exp <+> pprCallConv cconv <+> 
591         ppr ext_name <+> ppr_unsafe <+> ppr nm <+> dcolon <+> ppr ty
592         where
593          (ppr_imp_exp, ppr_unsafe) =
594            case imp_exp of
595              FoLabel     -> (ptext SLIT("label"), empty)
596              FoExport    -> (ptext SLIT("export"), empty)
597              FoImport us 
598                 | us        -> (ptext SLIT("import"), ptext SLIT("unsafe"))
599                 | otherwise -> (ptext SLIT("import"), empty)
600
601 data ForKind
602  = FoLabel
603  | FoExport
604  | FoImport Bool -- True  => unsafe call.
605
606 data ExtName
607  = Dynamic 
608  | ExtName CLabelString         -- The external name of the foreign thing,
609            (Maybe CLabelString) -- and optionally its DLL or module name
610                                 -- Both of these are completely unencoded; 
611                                 -- we just print them as they are
612
613 isDynamicExtName :: ExtName -> Bool
614 isDynamicExtName Dynamic = True
615 isDynamicExtName _       = False
616
617 extNameStatic :: ExtName -> CLabelString
618 extNameStatic (ExtName f _) = f
619 extNameStatic Dynamic       = panic "staticExtName: Dynamic - shouldn't ever happen."
620
621 instance Outputable ExtName where
622   ppr Dynamic      = ptext SLIT("dynamic")
623   ppr (ExtName nm mb_mod) = 
624      case mb_mod of { Nothing -> empty; Just m -> doubleQuotes (ptext m) } <+> 
625      doubleQuotes (pprCLabelString nm)
626 \end{code}
627
628 %************************************************************************
629 %*                                                                      *
630 \subsection{Transformation rules}
631 %*                                                                      *
632 %************************************************************************
633
634 \begin{code}
635 data RuleDecl name pat
636   = HsRule                      -- Source rule
637         FAST_STRING             -- Rule name
638         [name]                  -- Forall'd tyvars, filled in by the renamer with
639                                 -- tyvars mentioned in sigs; then filled out by typechecker
640         [RuleBndr name]         -- Forall'd term vars
641         (HsExpr name pat)       -- LHS
642         (HsExpr name pat)       -- RHS
643         SrcLoc          
644
645   | IfaceRule                   -- One that's come in from an interface file; pre-typecheck
646         FAST_STRING
647         [UfBinder name]         -- Tyvars and term vars
648         name                    -- Head of lhs
649         [UfExpr name]           -- Args of LHS
650         (UfExpr name)           -- Pre typecheck
651         SrcLoc          
652
653   | IfaceRuleOut                -- Post typecheck
654         name                    -- Head of LHS
655         CoreRule
656
657 isIfaceRuleDecl (HsRule _ _ _ _ _ _) = False
658 isIfaceRuleDecl other                = True
659
660 ifaceRuleDeclName :: RuleDecl name pat -> name
661 ifaceRuleDeclName (IfaceRule _ _ n _ _ _) = n
662 ifaceRuleDeclName (IfaceRuleOut n r)      = n
663 ifaceRuleDeclName (HsRule fs _ _ _ _ _)   = pprPanic "ifaceRuleDeclName" (ppr fs)
664
665 data RuleBndr name
666   = RuleBndr name
667   | RuleBndrSig name (HsType name)
668
669 instance (NamedThing name, Ord name) => Eq (RuleDecl name pat) where
670   -- Works for IfaceRules only; used when comparing interface file versions
671   (IfaceRule n1 bs1 f1 es1 rhs1 _) == (IfaceRule n2 bs2 f2 es2 rhs2 _)
672      = n1==n2 && f1 == f2 && 
673        eq_ufBinders emptyEqHsEnv bs1 bs2 (\env -> 
674        eqListBy (eq_ufExpr env) (rhs1:es1) (rhs2:es2))
675
676 instance (NamedThing name, Outputable name, Outputable pat)
677               => Outputable (RuleDecl name pat) where
678   ppr (HsRule name tvs ns lhs rhs loc)
679         = sep [text "{-# RULES" <+> doubleQuotes (ptext name),
680                pp_forall, ppr lhs, equals <+> ppr rhs,
681                text "#-}" ]
682         where
683           pp_forall | null tvs && null ns = empty
684                     | otherwise           = text "forall" <+> 
685                                             fsep (map ppr tvs ++ map ppr ns)
686                                             <> dot
687
688   ppr (IfaceRule name tpl_vars fn tpl_args rhs loc) 
689     = hsep [ doubleQuotes (ptext name),
690            ptext SLIT("__forall") <+> braces (interppSP tpl_vars),
691            ppr fn <+> sep (map (pprUfExpr parens) tpl_args),
692            ptext SLIT("=") <+> ppr rhs
693       ] <+> semi
694
695   ppr (IfaceRuleOut fn rule) = pprCoreRule (ppr fn) rule
696
697 instance Outputable name => Outputable (RuleBndr name) where
698    ppr (RuleBndr name) = ppr name
699    ppr (RuleBndrSig name ty) = ppr name <> dcolon <> ppr ty
700 \end{code}
701
702
703 %************************************************************************
704 %*                                                                      *
705 \subsection[DeprecDecl]{Deprecations}
706 %*                                                                      *
707 %************************************************************************
708
709 We use exported entities for things to deprecate.
710
711 \begin{code}
712 data DeprecDecl name = Deprecation name DeprecTxt SrcLoc
713
714 type DeprecTxt = FAST_STRING    -- reason/explanation for deprecation
715
716 instance Outputable name => Outputable (DeprecDecl name) where
717     ppr (Deprecation thing txt _)
718       = hsep [text "{-# DEPRECATED", ppr thing, doubleQuotes (ppr txt), text "#-}"]
719 \end{code}