Desugar.lhs 4.18 KB
Newer Older
1
%
2
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3
4
5
6
7
8
%
\section[Desugar]{@deSugar@: the main function}

\begin{code}
#include "HsVersions.h"

sof's avatar
sof committed
9
10
11
12
13
14
module Desugar ( deSugar, pprDsWarnings
#if __GLASGOW_HASKELL__ < 200
		, DsMatchContext
	        , DsWarnFlavour -- fluff needed for closure, 
				 -- removed when compiling with 1.4
#endif
15
	       ) where
16

17
IMP_Ubiq(){-uitous-}
18

sof's avatar
sof committed
19
20
21
22
23
import HsSyn		( HsBinds, HsExpr, MonoBinds,
			  SYN_IE(RecFlag), nonRecursive
			)
import TcHsSyn		( SYN_IE(TypecheckedHsBinds), SYN_IE(TypecheckedHsExpr)
			)
24
import CoreSyn
25
import Name             ( isExported )
26
import DsMonad
sof's avatar
sof committed
27
import DsBinds		( dsBinds, dsMonoBinds )
28
import DsUtils
29

30
import Bag		( unionBags )
31
32
33
34
import CmdLineOpts	( opt_DoCoreLinting, opt_AutoSccsOnAllToplevs, 
		          opt_AutoSccsOnExportedToplevs, opt_SccGroup
			   )
import CostCentre       ( IsCafCC(..), mkAutoCC )
35
36
import CoreLift		( liftCoreBindings )
import CoreLint		( lintCoreBindings )
sof's avatar
sof committed
37
38
import Id		( nullIdEnv, mkIdEnv, idType, 
			  SYN_IE(DictVar), GenId, SYN_IE(Id) )
sof's avatar
sof committed
39
import Outputable	( PprStyle(..) )
sof's avatar
sof committed
40
import UniqSupply	( splitUniqSupply, UniqSupply )
41
42
\end{code}

43
The only trick here is to get the @DsMonad@ stuff off to a good
44
45
46
start.

\begin{code}
47
deSugar :: UniqSupply		-- name supply
48
49
	-> FAST_STRING			-- module name

50
51
	-> (TypecheckedHsBinds, -- input: recsel, class, instance, and value
	    TypecheckedHsBinds, --   bindings; see "tcModule" (which produces
52
	    TypecheckedHsBinds,	--   them)
53
	    TypecheckedHsBinds,
sof's avatar
sof committed
54
	    TypecheckedHsBinds)
55
56
-- ToDo: handling of const_inst thingies is certainly WRONG ***************************

57
	-> ([CoreBinding],	-- output
58
	    DsWarnings)	    -- Shadowing complaints
59

sof's avatar
sof committed
60
deSugar us mod_name (recsel_binds, clas_binds, inst_binds, val_binds, const_inst_binds)
61
62
63
64
  = let
	(us0, us0a) = splitUniqSupply us
	(us1, us1a) = splitUniqSupply us0a
	(us2, us2a) = splitUniqSupply us1a
65
66
	(us3, us3a) = splitUniqSupply us2a
	(us4, us5)  = splitUniqSupply us3a
67

68
69
70
71
72

	module_and_group = (mod_name, grp_name)
	grp_name  = case opt_SccGroup of
		    	Just xx -> _PK_ xx
		    	Nothing -> mod_name	-- default: module name
73

sof's avatar
sof committed
74
75
76
	(core_const_binds, shadows1)
	    = initDs us0 nullIdEnv mod_name (dsBinds const_inst_binds)
	core_const_prs = pairsFromCoreBinds core_const_binds
77
78

	(core_clas_binds, shadows2)
sof's avatar
sof committed
79
			= initDs us1 nullIdEnv mod_name (dsBinds clas_binds)
80
	core_clas_prs	= pairsFromCoreBinds core_clas_binds
81

82
	(core_inst_binds, shadows3)
sof's avatar
sof committed
83
			= initDs us2 nullIdEnv mod_name (dsBinds inst_binds)
84
	core_inst_prs	= pairsFromCoreBinds core_inst_binds
85

86
	(core_val_binds, shadows4)
sof's avatar
sof committed
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)
sof's avatar
sof committed
91
			= initDs us4 nullIdEnv mod_name (dsBinds recsel_binds)
92
93
	core_recsel_prs	= pairsFromCoreBinds core_recsel_binds

94
    	final_binds
95
96
	  = if (null core_clas_prs && null core_inst_prs
	     && null core_recsel_prs {-???dont know???-} && null core_const_prs) then
97
98
99
100
		-- we don't have to make the whole thing recursive
		core_clas_binds ++ core_val_binds

	    else -- gotta make it recursive (sigh)
101
102
	       [Rec (core_clas_prs ++ core_inst_prs
		  ++ 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
108
109
			     then lintCoreBindings PprDebug "Desugarer" False lift_final_binds
			     else lift_final_binds

110
111
	shadows = shadows1 `unionBags` shadows2 `unionBags`
		  shadows3 `unionBags` shadows4 `unionBags` shadows5
112
113
114
    in
    (really_final_binds, shadows)
\end{code}
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140


%************************************************************************
%*									*
\subsection[addAutoScc]{Adding automatic sccs}
%*									*
%************************************************************************

\begin{code}
addAutoScc :: (FAST_STRING, FAST_STRING)	-- Module and group
	   -> (Id, CoreExpr)
	   -> (Id,CoreExpr)

addAutoScc (mod, grp) pair@(bndr, core_expr)
  | worthSCC core_expr &&
    (opt_AutoSccsOnAllToplevs ||
     (isExported bndr && opt_AutoSccsOnExportedToplevs))
  = (bndr, SCC (mkAutoCC bndr mod grp IsNotCafCC) core_expr)

  | otherwise
  = pair

worthSCC (SCC _ _) = False
worthSCC (Con _ _) = False
worthSCC core_expr = True
\end{code}