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, DsMatchContext, pprDsWarnings,
10 DsWarnFlavour -- removed when compiling with 1.4
15 import HsSyn ( HsBinds, HsExpr )
16 import TcHsSyn ( SYN_IE(TypecheckedHsBinds), SYN_IE(TypecheckedHsExpr) )
18 import Name ( isExported )
20 import DsBinds ( dsBinds, dsInstBinds )
23 import Bag ( unionBags )
24 import CmdLineOpts ( opt_DoCoreLinting, opt_AutoSccsOnAllToplevs,
25 opt_AutoSccsOnExportedToplevs, opt_SccGroup
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 )
35 The only trick here is to get the @DsMonad@ stuff off to a good
39 deSugar :: UniqSupply -- name supply
40 -> FAST_STRING -- module name
42 -> (TypecheckedHsBinds, -- input: recsel, class, instance, and value
43 TypecheckedHsBinds, -- bindings; see "tcModule" (which produces
44 TypecheckedHsBinds, -- them)
46 [(Id, TypecheckedHsExpr)])
47 -- ToDo: handling of const_inst thingies is certainly WRONG ***************************
49 -> ([CoreBinding], -- output
50 DsWarnings) -- Shadowing complaints
52 deSugar us mod_name (recsel_binds, clas_binds, inst_binds, val_binds, const_inst_pairs)
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
61 module_and_group = (mod_name, grp_name)
62 grp_name = case opt_SccGroup of
64 Nothing -> mod_name -- default: module name
66 ((core_const_prs, consts_pairs), shadows1)
67 = initDs us0 nullIdEnv mod_name (dsInstBinds [] const_inst_pairs)
69 consts_env = mkIdEnv consts_pairs
71 (core_clas_binds, shadows2)
72 = initDs us1 consts_env mod_name (dsBinds clas_binds)
73 core_clas_prs = pairsFromCoreBinds core_clas_binds
75 (core_inst_binds, shadows3)
76 = initDs us2 consts_env mod_name (dsBinds inst_binds)
77 core_inst_prs = pairsFromCoreBinds core_inst_binds
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)
83 (core_recsel_binds, shadows5)
84 = initDs us4 consts_env mod_name (dsBinds recsel_binds)
85 core_recsel_prs = pairsFromCoreBinds core_recsel_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
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)]
97 lift_final_binds = liftCoreBindings us5 final_binds
99 really_final_binds = if opt_DoCoreLinting
100 then lintCoreBindings PprDebug "Desugarer" False lift_final_binds
101 else lift_final_binds
103 shadows = shadows1 `unionBags` shadows2 `unionBags`
104 shadows3 `unionBags` shadows4 `unionBags` shadows5
106 (really_final_binds, shadows)
110 %************************************************************************
112 \subsection[addAutoScc]{Adding automatic sccs}
114 %************************************************************************
117 addAutoScc :: (FAST_STRING, FAST_STRING) -- Module and group
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)
130 worthSCC (SCC _ _) = False
131 worthSCC (Con _ _) = False
132 worthSCC core_expr = True