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