2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[Desugar]{@deSugar@: the main function}
7 #include "HsVersions.h"
9 module Desugar ( deSugar, pprDsWarnings
10 #if __GLASGOW_HASKELL__ < 200
12 , DsWarnFlavour -- fluff needed for closure,
13 -- removed when compiling with 1.4
19 import HsSyn ( HsBinds, HsExpr, MonoBinds,
20 SYN_IE(RecFlag), nonRecursive
22 import TcHsSyn ( SYN_IE(TypecheckedHsBinds), SYN_IE(TypecheckedHsExpr)
25 import Name ( isExported )
27 import DsBinds ( dsBinds, dsMonoBinds )
30 import Bag ( unionBags )
31 import CmdLineOpts ( opt_DoCoreLinting, opt_AutoSccsOnAllToplevs,
32 opt_AutoSccsOnExportedToplevs, opt_SccGroup
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 Outputable ( PprStyle(..) )
40 import UniqSupply ( splitUniqSupply, UniqSupply )
43 The only trick here is to get the @DsMonad@ stuff off to a good
47 deSugar :: UniqSupply -- name supply
48 -> FAST_STRING -- module name
50 -> (TypecheckedHsBinds, -- input: recsel, class, instance, and value
51 TypecheckedHsBinds, -- bindings; see "tcModule" (which produces
52 TypecheckedHsBinds, -- them)
55 -- ToDo: handling of const_inst thingies is certainly WRONG ***************************
57 -> ([CoreBinding], -- output
58 DsWarnings) -- Shadowing complaints
60 deSugar us mod_name (recsel_binds, clas_binds, inst_binds, val_binds, const_inst_binds)
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
69 module_and_group = (mod_name, grp_name)
70 grp_name = case opt_SccGroup of
72 Nothing -> mod_name -- default: module name
74 (core_const_binds, shadows1)
75 = initDs us0 nullIdEnv mod_name (dsBinds const_inst_binds)
76 core_const_prs = pairsFromCoreBinds core_const_binds
78 (core_clas_binds, shadows2)
79 = initDs us1 nullIdEnv mod_name (dsBinds clas_binds)
80 core_clas_prs = pairsFromCoreBinds core_clas_binds
82 (core_inst_binds, shadows3)
83 = initDs us2 nullIdEnv mod_name (dsBinds inst_binds)
84 core_inst_prs = pairsFromCoreBinds core_inst_binds
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)
90 (core_recsel_binds, shadows5)
91 = initDs us4 nullIdEnv mod_name (dsBinds recsel_binds)
92 core_recsel_prs = pairsFromCoreBinds core_recsel_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
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)]
104 lift_final_binds = liftCoreBindings us5 final_binds
106 really_final_binds = if opt_DoCoreLinting
107 then lintCoreBindings PprDebug "Desugarer" False lift_final_binds
108 else lift_final_binds
110 shadows = shadows1 `unionBags` shadows2 `unionBags`
111 shadows3 `unionBags` shadows4 `unionBags` shadows5
113 (really_final_binds, shadows)
117 %************************************************************************
119 \subsection[addAutoScc]{Adding automatic sccs}
121 %************************************************************************
124 addAutoScc :: (FAST_STRING, FAST_STRING) -- Module and group
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)
137 worthSCC (SCC _ _) = False
138 worthSCC (Con _ _) = False
139 worthSCC core_expr = True