[project @ 1997-06-13 04:11:47 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 BasicTypes       ( SYN_IE(Module) )
32 import CmdLineOpts      ( opt_DoCoreLinting, opt_SccGroup, opt_SccProfilingOn )
33 import CostCentre       ( IsCafCC(..), mkAutoCC )
34 import CoreLift         ( liftCoreBindings )
35 import CoreLint         ( lintCoreBindings )
36 import Id               ( nullIdEnv, mkIdEnv, idType, 
37                           SYN_IE(DictVar), GenId, SYN_IE(Id) )
38 import Outputable       ( PprStyle(..) )
39 import UniqSupply       ( splitUniqSupply, UniqSupply )
40 \end{code}
41
42 The only trick here is to get the @DsMonad@ stuff off to a good
43 start.
44
45 \begin{code}
46 deSugar :: UniqSupply           -- name supply
47         -> Module               -- module name
48
49         -> (TypecheckedHsBinds, -- input: recsel, class, instance, and value
50             TypecheckedHsBinds, --   bindings; see "tcModule" (which produces
51             TypecheckedHsBinds, --   them)
52             TypecheckedHsBinds,
53             TypecheckedHsBinds)
54 -- ToDo: handling of const_inst thingies is certainly WRONG ***************************
55
56         -> ([CoreBinding],      -- output
57             DsWarnings)     -- Shadowing complaints
58
59 deSugar us mod_name (recsel_binds, clas_binds, inst_binds, val_binds, const_inst_binds)
60   = let
61         (us0, us0a) = splitUniqSupply us
62         (us1, us1a) = splitUniqSupply us0a
63         (us2, us2a) = splitUniqSupply us1a
64         (us3, us3a) = splitUniqSupply us2a
65         (us4, us5)  = splitUniqSupply us3a
66
67         module_and_group = (mod_name, grp_name)
68
69         grp_name  = case opt_SccGroup of
70                         Just xx -> _PK_ xx
71                         Nothing -> mod_name     -- default: module name
72
73         (core_const_binds, shadows1)
74             = initDs us0 nullIdEnv module_and_group (dsBinds False const_inst_binds)
75         core_const_prs = pairsFromCoreBinds core_const_binds
76
77         (core_clas_binds, shadows2)
78                         = initDs us1 nullIdEnv module_and_group (dsBinds False clas_binds)
79         core_clas_prs   = pairsFromCoreBinds core_clas_binds
80
81         (core_inst_binds, shadows3)
82                         = initDs us2 nullIdEnv module_and_group (dsBinds False inst_binds)
83         core_inst_prs   = pairsFromCoreBinds core_inst_binds
84
85         (core_val_binds, shadows4)
86                         = initDs us3 nullIdEnv module_and_group (dsBinds opt_SccProfilingOn val_binds)
87         core_val_pairs  = pairsFromCoreBinds core_val_binds
88
89         (core_recsel_binds, shadows5)
90                         = initDs us4 nullIdEnv module_and_group (dsBinds False recsel_binds)
91         core_recsel_prs = pairsFromCoreBinds core_recsel_binds
92
93         final_binds
94           = if (null core_clas_prs && null core_inst_prs
95              && null core_recsel_prs {-???dont know???-} && null core_const_prs) then
96                 -- we don't have to make the whole thing recursive
97                 core_clas_binds ++ core_val_binds
98
99             else -- gotta make it recursive (sigh)
100                [Rec (core_clas_prs ++ core_inst_prs
101                   ++ core_const_prs ++ core_val_pairs ++ core_recsel_prs)]
102
103         lift_final_binds = liftCoreBindings us5 final_binds
104
105         really_final_binds = if opt_DoCoreLinting
106                              then lintCoreBindings PprDebug "Desugarer" False lift_final_binds
107                              else lift_final_binds
108
109         shadows = shadows1 `unionBags` shadows2 `unionBags`
110                   shadows3 `unionBags` shadows4 `unionBags` shadows5
111     in
112     (really_final_binds, shadows)
113 \end{code}