[project @ 1997-05-19 00:12:10 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, dsMonoBinds )
28 import DsUtils
29
30 import Bag              ( unionBags )
31 import CmdLineOpts      ( opt_DoCoreLinting, opt_AutoSccsOnAllToplevs, 
32                           opt_AutoSccsOnExportedToplevs, opt_SccGroup
33                            )
34 import CostCentre       ( IsCafCC(..), mkAutoCC )
35 import CoreLift         ( liftCoreBindings )
36 import CoreLint         ( lintCoreBindings )
37 import Id               ( nullIdEnv, mkIdEnv, idType, 
38                           SYN_IE(DictVar), GenId, SYN_IE(Id) )
39 import PprStyle         ( PprStyle(..) )
40 import UniqSupply       ( splitUniqSupply, UniqSupply )
41 \end{code}
42
43 The only trick here is to get the @DsMonad@ stuff off to a good
44 start.
45
46 \begin{code}
47 deSugar :: UniqSupply           -- name supply
48         -> FAST_STRING                  -- module name
49
50         -> (TypecheckedHsBinds, -- input: recsel, class, instance, and value
51             TypecheckedHsBinds, --   bindings; see "tcModule" (which produces
52             TypecheckedHsBinds, --   them)
53             TypecheckedHsBinds,
54             TypecheckedHsBinds)
55 -- ToDo: handling of const_inst thingies is certainly WRONG ***************************
56
57         -> ([CoreBinding],      -- output
58             DsWarnings)     -- Shadowing complaints
59
60 deSugar us mod_name (recsel_binds, clas_binds, inst_binds, val_binds, const_inst_binds)
61   = let
62         (us0, us0a) = splitUniqSupply us
63         (us1, us1a) = splitUniqSupply us0a
64         (us2, us2a) = splitUniqSupply us1a
65         (us3, us3a) = splitUniqSupply us2a
66         (us4, us5)  = splitUniqSupply us3a
67
68
69         module_and_group = (mod_name, grp_name)
70         grp_name  = case opt_SccGroup of
71                         Just xx -> _PK_ xx
72                         Nothing -> mod_name     -- default: module name
73
74         (core_const_binds, shadows1)
75             = initDs us0 nullIdEnv mod_name (dsBinds const_inst_binds)
76         core_const_prs = pairsFromCoreBinds core_const_binds
77
78         (core_clas_binds, shadows2)
79                         = initDs us1 nullIdEnv mod_name (dsBinds clas_binds)
80         core_clas_prs   = pairsFromCoreBinds core_clas_binds
81
82         (core_inst_binds, shadows3)
83                         = initDs us2 nullIdEnv mod_name (dsBinds inst_binds)
84         core_inst_prs   = pairsFromCoreBinds core_inst_binds
85
86         (core_val_binds, shadows4)
87                         = initDs us3 nullIdEnv mod_name (dsBinds val_binds)
88         core_val_pairs  = map (addAutoScc module_and_group) (pairsFromCoreBinds core_val_binds)
89
90         (core_recsel_binds, shadows5)
91                         = initDs us4 nullIdEnv mod_name (dsBinds recsel_binds)
92         core_recsel_prs = pairsFromCoreBinds core_recsel_binds
93
94         final_binds
95           = if (null core_clas_prs && null core_inst_prs
96              && null core_recsel_prs {-???dont know???-} && null core_const_prs) then
97                 -- we don't have to make the whole thing recursive
98                 core_clas_binds ++ core_val_binds
99
100             else -- gotta make it recursive (sigh)
101                [Rec (core_clas_prs ++ core_inst_prs
102                   ++ core_const_prs ++ core_val_pairs ++ core_recsel_prs)]
103
104         lift_final_binds = liftCoreBindings us5 final_binds
105
106         really_final_binds = if opt_DoCoreLinting
107                              then lintCoreBindings PprDebug "Desugarer" False lift_final_binds
108                              else lift_final_binds
109
110         shadows = shadows1 `unionBags` shadows2 `unionBags`
111                   shadows3 `unionBags` shadows4 `unionBags` shadows5
112     in
113     (really_final_binds, shadows)
114 \end{code}
115
116
117 %************************************************************************
118 %*                                                                      *
119 \subsection[addAutoScc]{Adding automatic sccs}
120 %*                                                                      *
121 %************************************************************************
122
123 \begin{code}
124 addAutoScc :: (FAST_STRING, FAST_STRING)        -- Module and group
125            -> (Id, CoreExpr)
126            -> (Id,CoreExpr)
127
128 addAutoScc (mod, grp) pair@(bndr, core_expr)
129   | worthSCC core_expr &&
130     (opt_AutoSccsOnAllToplevs ||
131      (isExported bndr && opt_AutoSccsOnExportedToplevs))
132   = (bndr, SCC (mkAutoCC bndr mod grp IsNotCafCC) core_expr)
133
134   | otherwise
135   = pair
136
137 worthSCC (SCC _ _) = False
138 worthSCC (Con _ _) = False
139 worthSCC core_expr = True
140 \end{code}