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