7fe9bf4125c277b9975dbb43f621502b09797319
[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,
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 tyClDeclName :: TyClDecl name pat -> name
219 tyClDeclName (IfaceSig name _ _ _)           = name
220 tyClDeclName (TyData _ _ name _ _ _ _ _ _ _) = name
221 tyClDeclName (TySynonym name _ _ _)          = name
222 tyClDeclName (ClassDecl _ name _ _ _ _ _ _)  = name
223
224
225 tyClDeclNames :: Eq name => TyClDecl name pat -> [(name, SrcLoc)]
226 -- Returns all the binding names of the decl, along with their SrcLocs
227 -- The first one is guaranteed to be the name of the decl
228 -- For record fields, the first one counts as the SrcLoc
229 -- We use the equality to filter out duplicate field names
230
231 tyClDeclNames (TySynonym name _ _ loc)
232   = [(name,loc)]
233
234 tyClDeclNames (ClassDecl _ cls_name _ _ sigs _ _ loc)
235   = (cls_name,loc) : [(n,loc) | ClassOpSig n _ _ loc <- sigs]
236
237 tyClDeclNames (TyData _ _ tc_name _ cons _ _ loc _ _)
238   = (tc_name,loc) : conDeclsNames cons
239
240 tyClDeclNames (IfaceSig name _ _ loc) = [(name,loc)]
241
242 type ClassDeclSysNames name = [name]
243         --      [tycon, datacon wrapper, datacon worker, 
244         --       superclass selector 1, ..., superclass selector n]
245         -- They are kept in a list rather than a tuple to make the
246         -- renamer easier.
247
248 mkClassDeclSysNames  :: (name, name, name, [name]) -> [name]
249 getClassDeclSysNames :: [name] -> (name, name, name, [name])
250 mkClassDeclSysNames  (a,b,c,ds) = a:b:c:ds
251 getClassDeclSysNames (a:b:c:ds) = (a,b,c,ds)
252 \end{code}
253
254 \begin{code}
255 instance (NamedThing name, Ord name) => Eq (TyClDecl name pat) where
256         -- Used only when building interface files
257   (==) (IfaceSig n1 t1 i1 _)
258        (IfaceSig n2 t2 i2 _) = n1==n2 && t1==t2 && i1==i2
259
260   (==) (TyData nd1 cxt1 n1 tvs1 cons1 _ _ _ _ _)
261        (TyData nd2 cxt2 n2 tvs2 cons2 _ _ _ _ _)
262     = n1 == n2 &&
263       nd1 == nd2 &&
264       eqWithHsTyVars tvs1 tvs2 (\ env -> 
265           eq_hsContext env cxt1 cxt2  &&
266           eqListBy (eq_ConDecl env) cons1 cons2
267       )
268
269   (==) (TySynonym n1 tvs1 ty1 _)
270        (TySynonym n2 tvs2 ty2 _)
271     =  n1 == n2 &&
272        eqWithHsTyVars tvs1 tvs2 (\ env -> eq_hsType env ty1 ty2)
273
274   (==) (ClassDecl cxt1 n1 tvs1 fds1 sigs1 _ _ _ )
275        (ClassDecl cxt2 n2 tvs2 fds2 sigs2 _ _ _ )
276     =  n1 == n2 &&
277        eqWithHsTyVars tvs1 tvs2 (\ env -> 
278           eq_hsContext env cxt1 cxt2 &&
279           eqListBy (eq_hsFD env) fds1 fds2 &&
280           eqListBy (eq_cls_sig env) sigs1 sigs2
281        )
282
283   (==) _ _ = False      -- default case
284
285 eq_hsFD env (ns1,ms1) (ns2,ms2)
286   = eqListBy (eq_hsVar env) ns1 ns2 && eqListBy (eq_hsVar env) ms1 ms2
287
288 eq_cls_sig env (ClassOpSig n1 dm1 ty1 _) (ClassOpSig n2 dm2 ty2 _)
289   = n1==n2 && dm1 `eq_dm` dm2 && eq_hsType env ty1 ty2
290   where
291         -- Ignore the name of the default method for (DefMeth id)
292         -- This is used for comparing declarations before putting
293         -- them into interface files, and the name of the default 
294         -- method isn't relevant
295     Nothing            `eq_dm` Nothing            = True
296     (Just NoDefMeth)   `eq_dm` (Just NoDefMeth)   = True
297     (Just GenDefMeth)  `eq_dm` (Just GenDefMeth)  = True
298     (Just (DefMeth _)) `eq_dm` (Just (DefMeth _)) = True
299     dm1                `eq_dm` dm2                = False
300
301     
302 \end{code}
303
304 \begin{code}
305 countTyClDecls :: [TyClDecl name pat] -> (Int, Int, Int, Int, Int)
306         -- class, data, newtype, synonym decls
307 countTyClDecls decls 
308  = (length [() | ClassDecl _ _ _ _ _ _ _ _         <- decls],
309     length [() | TyData DataType _ _ _ _ _ _ _ _ _ <- decls],
310     length [() | TyData NewType  _ _ _ _ _ _ _ _ _ <- decls],
311     length [() | TySynonym _ _ _ _                 <- decls],
312     length [() | IfaceSig _ _ _ _                  <- decls])
313 \end{code}
314
315 \begin{code}
316 instance (NamedThing name, Outputable name, Outputable pat)
317               => Outputable (TyClDecl name pat) where
318
319     ppr (IfaceSig var ty info _) = hsep [ppr var, dcolon, ppr ty, pprHsIdInfo info]
320
321     ppr (TySynonym tycon tyvars mono_ty src_loc)
322       = hang (ptext SLIT("type") <+> pp_decl_head [] tycon tyvars <+> equals)
323              4 (ppr mono_ty)
324
325     ppr (TyData new_or_data context tycon tyvars condecls ncons 
326                 derivings src_loc gen_conv1 gen_conv2) -- The generic names are not printed out ATM
327       = pp_tydecl
328                   (ptext keyword <+> pp_decl_head context tycon tyvars <+> equals)
329                   (pp_condecls condecls ncons)
330                   derivings
331       where
332         keyword = case new_or_data of
333                         NewType  -> SLIT("newtype")
334                         DataType -> SLIT("data")
335
336     ppr (ClassDecl context clas tyvars fds sigs methods _ src_loc)
337       | null sigs       -- No "where" part
338       = top_matter
339
340       | otherwise       -- Laid out
341       = sep [hsep [top_matter, ptext SLIT("where {")],
342              nest 4 (sep [sep (map ppr_sig sigs), pp_methods, char '}'])]
343       where
344         top_matter  = ptext SLIT("class") <+> pp_decl_head context clas tyvars <+> pprFundeps fds
345         ppr_sig sig = ppr sig <> semi
346         pp_methods = getPprStyle $ \ sty ->
347                      if ifaceStyle sty then empty else ppr methods
348         
349 pp_decl_head :: Outputable name => HsContext name -> name -> [HsTyVarBndr name] -> SDoc
350 pp_decl_head context thing tyvars = hsep [pprHsContext context, ppr thing, interppSP tyvars]
351
352 pp_condecls []     ncons = ptext SLIT("{- abstract with") <+> int ncons <+> ptext SLIT("constructors -}")
353 pp_condecls (c:cs) ncons = sep (ppr c : map (\ c -> ptext SLIT("|") <+> ppr c) cs)
354
355 pp_tydecl pp_head pp_decl_rhs derivings
356   = hang pp_head 4 (sep [
357         pp_decl_rhs,
358         case derivings of
359           Nothing          -> empty
360           Just ds          -> hsep [ptext SLIT("deriving"), parens (interpp'SP ds)]
361     ])
362 \end{code}
363
364
365 %************************************************************************
366 %*                                                                      *
367 \subsection[ConDecl]{A data-constructor declaration}
368 %*                                                                      *
369 %************************************************************************
370
371 \begin{code}
372 data ConDecl name
373   = ConDecl     name                    -- Constructor name; this is used for the
374                                         -- DataCon itself, and for the user-callable wrapper Id
375
376                 name                    -- Name of the constructor's 'worker Id'
377                                         -- Filled in as the ConDecl is built
378
379                 [HsTyVarBndr name]      -- Existentially quantified type variables
380                 (HsContext name)        -- ...and context
381                                         -- If both are empty then there are no existentials
382
383                 (ConDetails name)
384                 SrcLoc
385
386 data ConDetails name
387   = VanillaCon                  -- prefix-style con decl
388                 [BangType name]
389
390   | InfixCon                    -- infix-style con decl
391                 (BangType name)
392                 (BangType name)
393
394   | RecCon                      -- record-style con decl
395                 [([name], BangType name)]       -- list of "fields"
396 \end{code}
397
398 \begin{code}
399 conDeclsNames :: Eq name => [ConDecl name] -> [(name,SrcLoc)]
400   -- See tyClDeclNames for what this does
401   -- The function is boringly complicated because of the records
402   -- And since we only have equality, we have to be a little careful
403 conDeclsNames cons
404   = snd (foldl do_one ([], []) cons)
405   where
406     do_one (flds_seen, acc) (ConDecl name _ _ _ details loc)
407         = do_details ((name,loc):acc) details
408         where
409           do_details acc (RecCon flds) = foldl do_fld (flds_seen, acc) flds
410           do_details acc other         = (flds_seen, acc)
411
412           do_fld acc (flds, _) = foldl do_fld1 acc flds
413
414           do_fld1 (flds_seen, acc) fld
415                 | fld `elem` flds_seen = (flds_seen,acc)
416                 | otherwise            = (fld:flds_seen, (fld,loc):acc)
417 \end{code}
418
419 \begin{code}
420 conDetailsTys :: ConDetails name -> [HsType name]
421 conDetailsTys (VanillaCon btys)    = map getBangType btys
422 conDetailsTys (InfixCon bty1 bty2) = [getBangType bty1, getBangType bty2]
423 conDetailsTys (RecCon fields)      = [getBangType bty | (_, bty) <- fields]
424
425
426 eq_ConDecl env (ConDecl n1 _ tvs1 cxt1 cds1 _)
427                (ConDecl n2 _ tvs2 cxt2 cds2 _)
428   = n1 == n2 &&
429     (eq_hsTyVars env tvs1 tvs2  $ \ env ->
430      eq_hsContext env cxt1 cxt2 &&
431      eq_ConDetails env cds1 cds2)
432
433 eq_ConDetails env (VanillaCon bts1) (VanillaCon bts2)
434   = eqListBy (eq_btype env) bts1 bts2
435 eq_ConDetails env (InfixCon bta1 btb1) (InfixCon bta2 btb2)
436   = eq_btype env bta1 bta2 && eq_btype env btb1 btb2
437 eq_ConDetails env (RecCon fs1) (RecCon fs2)
438   = eqListBy (eq_fld env) fs1 fs2
439 eq_ConDetails env _ _ = False
440
441 eq_fld env (ns1,bt1) (ns2, bt2) = ns1==ns2 && eq_btype env bt1 bt2
442 \end{code}
443   
444 \begin{code}
445 data BangType name
446   = Banged   (HsType name)      -- HsType: to allow Haskell extensions
447   | Unbanged (HsType name)      -- (MonoType only needed for straight Haskell)
448   | Unpacked (HsType name)      -- Field is strict and to be unpacked if poss.
449
450 getBangType (Banged ty)   = ty
451 getBangType (Unbanged ty) = ty
452 getBangType (Unpacked ty) = ty
453
454 eq_btype env (Banged t1)   (Banged t2)   = eq_hsType env t1 t2
455 eq_btype env (Unbanged t1) (Unbanged t2) = eq_hsType env t1 t2
456 eq_btype env (Unpacked t1) (Unpacked t2) = eq_hsType env t1 t2
457 eq_btype env _             _             = False
458 \end{code}
459
460 \begin{code}
461 instance (Outputable name) => Outputable (ConDecl name) where
462     ppr (ConDecl con _ tvs cxt con_details  loc)
463       = sep [pprHsForAll tvs cxt, ppr_con_details con con_details]
464
465 ppr_con_details con (InfixCon ty1 ty2)
466   = hsep [ppr_bang ty1, ppr con, ppr_bang ty2]
467
468 ppr_con_details con (VanillaCon tys)
469   = ppr con <+> hsep (map (ppr_bang) tys)
470
471 ppr_con_details con (RecCon fields)
472   = ppr con <+> braces (hsep (punctuate comma (map ppr_field fields)))
473   where
474     ppr_field (ns, ty) = hsep (map (ppr) ns) <+> 
475                          dcolon <+>
476                          ppr_bang ty
477
478 instance Outputable name => Outputable (BangType name) where
479     ppr = ppr_bang
480
481 ppr_bang (Banged   ty) = ptext SLIT("!") <> pprParendHsType ty
482 ppr_bang (Unbanged ty) = pprParendHsType ty
483 ppr_bang (Unpacked ty) = ptext SLIT("! !") <> pprParendHsType ty
484 \end{code}
485
486
487 %************************************************************************
488 %*                                                                      *
489 \subsection[InstDecl]{An instance declaration
490 %*                                                                      *
491 %************************************************************************
492
493 \begin{code}
494 data InstDecl name pat
495   = InstDecl    (HsType name)   -- Context => Class Instance-type
496                                 -- Using a polytype means that the renamer conveniently
497                                 -- figures out the quantified type variables for us.
498
499                 (MonoBinds name pat)
500
501                 [Sig name]              -- User-supplied pragmatic info
502
503                 (Maybe name)            -- Name for the dictionary function
504                                         -- Nothing for source-file instance decls
505
506                 SrcLoc
507 \end{code}
508
509 \begin{code}
510 instance (Outputable name, Outputable pat)
511               => Outputable (InstDecl name pat) where
512
513     ppr (InstDecl inst_ty binds uprags maybe_dfun_name src_loc)
514       = getPprStyle $ \ sty ->
515         if ifaceStyle sty then
516            hsep [ptext SLIT("instance"), ppr inst_ty, equals, pp_dfun]
517         else
518            vcat [hsep [ptext SLIT("instance"), ppr inst_ty, ptext SLIT("where")],
519                  nest 4 (ppr uprags),
520                  nest 4 (ppr binds) ]
521       where
522         pp_dfun = case maybe_dfun_name of
523                     Just df -> ppr df
524                     Nothing -> empty
525 \end{code}
526
527 \begin{code}
528 instance Ord name => Eq (InstDecl name pat) where
529         -- Used for interface comparison only, so don't compare bindings
530   (==) (InstDecl inst_ty1 _ _ dfun1 _) (InstDecl inst_ty2 _ _ dfun2 _)
531        = inst_ty1 == inst_ty2 && dfun1 == dfun2
532 \end{code}
533
534
535 %************************************************************************
536 %*                                                                      *
537 \subsection[DefaultDecl]{A @default@ declaration}
538 %*                                                                      *
539 %************************************************************************
540
541 There can only be one default declaration per module, but it is hard
542 for the parser to check that; we pass them all through in the abstract
543 syntax, and that restriction must be checked in the front end.
544
545 \begin{code}
546 data DefaultDecl name
547   = DefaultDecl [HsType name]
548                 SrcLoc
549
550 instance (Outputable name)
551               => Outputable (DefaultDecl name) where
552
553     ppr (DefaultDecl tys src_loc)
554       = ptext SLIT("default") <+> parens (interpp'SP tys)
555 \end{code}
556
557 %************************************************************************
558 %*                                                                      *
559 \subsection{Foreign function interface declaration}
560 %*                                                                      *
561 %************************************************************************
562
563 \begin{code}
564 data ForeignDecl name = 
565    ForeignDecl 
566         name 
567         ForKind   
568         (HsType name)
569         ExtName
570         CallConv
571         SrcLoc
572
573 instance (Outputable name)
574               => Outputable (ForeignDecl name) where
575
576     ppr (ForeignDecl nm imp_exp ty ext_name cconv src_loc)
577       = ptext SLIT("foreign") <+> ppr_imp_exp <+> pprCallConv cconv <+> 
578         ppr ext_name <+> ppr_unsafe <+> ppr nm <+> dcolon <+> ppr ty
579         where
580          (ppr_imp_exp, ppr_unsafe) =
581            case imp_exp of
582              FoLabel     -> (ptext SLIT("label"), empty)
583              FoExport    -> (ptext SLIT("export"), empty)
584              FoImport us 
585                 | us        -> (ptext SLIT("import"), ptext SLIT("unsafe"))
586                 | otherwise -> (ptext SLIT("import"), empty)
587
588 data ForKind
589  = FoLabel
590  | FoExport
591  | FoImport Bool -- True  => unsafe call.
592
593 data ExtName
594  = Dynamic 
595  | ExtName CLabelString         -- The external name of the foreign thing,
596            (Maybe CLabelString) -- and optionally its DLL or module name
597                                 -- Both of these are completely unencoded; 
598                                 -- we just print them as they are
599
600 isDynamicExtName :: ExtName -> Bool
601 isDynamicExtName Dynamic = True
602 isDynamicExtName _       = False
603
604 extNameStatic :: ExtName -> CLabelString
605 extNameStatic (ExtName f _) = f
606 extNameStatic Dynamic       = panic "staticExtName: Dynamic - shouldn't ever happen."
607
608 instance Outputable ExtName where
609   ppr Dynamic      = ptext SLIT("dynamic")
610   ppr (ExtName nm mb_mod) = 
611      case mb_mod of { Nothing -> empty; Just m -> doubleQuotes (ptext m) } <+> 
612      doubleQuotes (pprCLabelString nm)
613 \end{code}
614
615 %************************************************************************
616 %*                                                                      *
617 \subsection{Transformation rules}
618 %*                                                                      *
619 %************************************************************************
620
621 \begin{code}
622 data RuleDecl name pat
623   = HsRule                      -- Source rule
624         FAST_STRING             -- Rule name
625         [name]                  -- Forall'd tyvars, filled in by the renamer with
626                                 -- tyvars mentioned in sigs; then filled out by typechecker
627         [RuleBndr name]         -- Forall'd term vars
628         (HsExpr name pat)       -- LHS
629         (HsExpr name pat)       -- RHS
630         SrcLoc          
631
632   | IfaceRule                   -- One that's come in from an interface file; pre-typecheck
633         FAST_STRING
634         [UfBinder name]         -- Tyvars and term vars
635         name                    -- Head of lhs
636         [UfExpr name]           -- Args of LHS
637         (UfExpr name)           -- Pre typecheck
638         SrcLoc          
639
640   | IfaceRuleOut                -- Post typecheck
641         name                    -- Head of LHS
642         CoreRule
643
644 isIfaceRuleDecl (HsRule _ _ _ _ _ _) = False
645 isIfaceRuleDecl other                = True
646
647 ifaceRuleDeclName :: RuleDecl name pat -> name
648 ifaceRuleDeclName (IfaceRule _ _ n _ _ _) = n
649 ifaceRuleDeclName (IfaceRuleOut n r)      = n
650 ifaceRuleDeclName (HsRule fs _ _ _ _ _)   = pprPanic "ifaceRuleDeclName" (ppr fs)
651
652 data RuleBndr name
653   = RuleBndr name
654   | RuleBndrSig name (HsType name)
655
656 instance (NamedThing name, Ord name) => Eq (RuleDecl name pat) where
657   -- Works for IfaceRules only; used when comparing interface file versions
658   (IfaceRule n1 bs1 f1 es1 rhs1 _) == (IfaceRule n2 bs2 f2 es2 rhs2 _)
659      = n1==n2 && f1 == f2 && 
660        eq_ufBinders emptyEqHsEnv bs1 bs2 (\env -> 
661        eqListBy (eq_ufExpr env) (rhs1:es1) (rhs2:es2))
662
663 instance (NamedThing name, Outputable name, Outputable pat)
664               => Outputable (RuleDecl name pat) where
665   ppr (HsRule name tvs ns lhs rhs loc)
666         = sep [text "{-# RULES" <+> doubleQuotes (ptext name),
667                pp_forall, ppr lhs, equals <+> ppr rhs,
668                text "#-}" ]
669         where
670           pp_forall | null tvs && null ns = empty
671                     | otherwise           = text "forall" <+> 
672                                             fsep (map ppr tvs ++ map ppr ns)
673                                             <> dot
674
675   ppr (IfaceRule name tpl_vars fn tpl_args rhs loc) 
676     = hsep [ doubleQuotes (ptext name),
677            ptext SLIT("__forall") <+> braces (interppSP tpl_vars),
678            ppr fn <+> sep (map (pprUfExpr parens) tpl_args),
679            ptext SLIT("=") <+> ppr rhs
680       ] <+> semi
681
682   ppr (IfaceRuleOut fn rule) = pprCoreRule (ppr fn) rule
683
684 instance Outputable name => Outputable (RuleBndr name) where
685    ppr (RuleBndr name) = ppr name
686    ppr (RuleBndrSig name ty) = ppr name <> dcolon <> ppr ty
687 \end{code}
688
689
690 %************************************************************************
691 %*                                                                      *
692 \subsection[DeprecDecl]{Deprecations}
693 %*                                                                      *
694 %************************************************************************
695
696 We use exported entities for things to deprecate.
697
698 \begin{code}
699 data DeprecDecl name = Deprecation name DeprecTxt SrcLoc
700
701 type DeprecTxt = FAST_STRING    -- reason/explanation for deprecation
702
703 instance Outputable name => Outputable (DeprecDecl name) where
704     ppr (Deprecation thing txt _)
705       = hsep [text "{-# DEPRECATED", ppr thing, doubleQuotes (ppr txt), text "#-}"]
706 \end{code}