[project @ 1997-06-05 21:08:51 by sof]
[ghc-hetmet.git] / ghc / compiler / deSugar / Desugar.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[Desugar]{@deSugar@: the main function}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module Desugar ( deSugar, pprDsWarnings
10 #if __GLASGOW_HASKELL__ < 200
11                 , DsMatchContext
12                 , DsWarnFlavour -- fluff needed for closure, 
13                                  -- removed when compiling with 1.4
14 #endif
15                ) where
16
17 IMP_Ubiq(){-uitous-}
18
19 import HsSyn            ( HsBinds, HsExpr, MonoBinds,
20                           SYN_IE(RecFlag), nonRecursive
21                         )
22 import TcHsSyn          ( SYN_IE(TypecheckedHsBinds), SYN_IE(TypecheckedHsExpr)
23                         )
24 import CoreSyn
25 import Name             ( isExported )
26 import DsMonad
27 import DsBinds          ( dsBinds )
28 import DsUtils
29
30 import Bag              ( unionBags )
31 import CmdLineOpts      ( opt_DoCoreLinting, opt_SccGroup )
32 import CostCentre       ( IsCafCC(..), mkAutoCC )
33 import CoreLift         ( liftCoreBindings )
34 import CoreLint         ( lintCoreBindings )
35 import Id               ( nullIdEnv, mkIdEnv, idType, 
36                           SYN_IE(DictVar), GenId, SYN_IE(Id) )
37 import Outputable       ( PprStyle(..) )
38 import UniqSupply       ( splitUniqSupply, UniqSupply )
39 \end{code}
40
41 The only trick here is to get the @DsMonad@ stuff off to a good
42 start.
43
44 \begin{code}
45 deSugar :: UniqSupply           -- name supply
46         -> FAST_STRING                  -- module name
47
48         -> (TypecheckedHsBinds, -- input: recsel, class, instance, and value
49             TypecheckedHsBinds, --   bindings; see "tcModule" (which produces
50             TypecheckedHsBinds, --   them)
51             TypecheckedHsBinds,
52             TypecheckedHsBinds)
53 -- ToDo: handling of const_inst thingies is certainly WRONG ***************************
54
55         -> ([CoreBinding],      -- output
56             DsWarnings)     -- Shadowing complaints
57
58 deSugar us mod_name (recsel_binds, clas_binds, inst_binds, val_binds, const_inst_binds)
59   = let
60         (us0, us0a) = splitUniqSupply us
61         (us1, us1a) = splitUniqSupply us0a
62         (us2, us2a) = splitUniqSupply us1a
63         (us3, us3a) = splitUniqSupply us2a
64         (us4, us5)  = splitUniqSupply us3a
65
66
67         module_and_group = (mod_name, grp_name)
68         grp_name  = case opt_SccGroup of
69                         Just xx -> _PK_ xx
70                         Nothing -> mod_name     -- default: module name
71
72         (core_const_binds, shadows1)
73             = initDs us0 nullIdEnv mod_name (dsBinds Nothing const_inst_binds)
74         core_const_prs = pairsFromCoreBinds core_const_binds
75
76         (core_clas_binds, shadows2)
77                         = initDs us1 nullIdEnv mod_name (dsBinds Nothing clas_binds)
78         core_clas_prs   = pairsFromCoreBinds core_clas_binds
79
80         (core_inst_binds, shadows3)
81                         = initDs us2 nullIdEnv mod_name (dsBinds Nothing inst_binds)
82         core_inst_prs   = pairsFromCoreBinds core_inst_binds
83
84         (core_val_binds, shadows4)
85                         = initDs us3 nullIdEnv mod_name (dsBinds (Just module_and_group) val_binds)
86         core_val_pairs  = pairsFromCoreBinds core_val_binds
87
88         (core_recsel_binds, shadows5)
89                         = initDs us4 nullIdEnv mod_name (dsBinds Nothing recsel_binds)
90         core_recsel_prs = pairsFromCoreBinds core_recsel_binds
91
92         final_binds
93           = if (null core_clas_prs && null core_inst_prs
94              && null core_recsel_prs {-???dont know???-} && null core_const_prs) then
95                 -- we don't have to make the whole thing recursive
96                 core_clas_binds ++ core_val_binds
97
98             else -- gotta make it recursive (sigh)
99                [Rec (core_clas_prs ++ core_inst_prs
100                   ++ core_const_prs ++ core_val_pairs ++ core_recsel_prs)]
101
102         lift_final_binds = liftCoreBindings us5 final_binds
103
104         really_final_binds = if opt_DoCoreLinting
105                              then lintCoreBindings PprDebug "Desugarer" False lift_final_binds
106                              else lift_final_binds
107
108         shadows = shadows1 `unionBags` shadows2 `unionBags`
109                   shadows3 `unionBags` shadows4 `unionBags` shadows5
110     in
111     (really_final_binds, shadows)
112 \end{code}