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