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