[project @ 2000-11-24 17:02:01 by simonpj]
[ghc-hetmet.git] / ghc / compiler / main / HscStats.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[GHC_Stats]{Statistics for per-module compilations}
5
6 \begin{code}
7 module HscStats ( ppSourceStats ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn
12 import Outputable
13 import Char             ( isSpace )
14 \end{code}
15
16 %************************************************************************
17 %*                                                                      *
18 \subsection{Statistics}
19 %*                                                                      *
20 %************************************************************************
21
22 \begin{code}
23 ppSourceStats short (HsModule name version exports imports decls _ src_loc)
24  = (if short then hcat else vcat)
25         (map pp_val
26                [("ExportAll        ", export_all), -- 1 if no export list
27                 ("ExportDecls      ", export_ds),
28                 ("ExportModules    ", export_ms),
29                 ("Imports          ", import_no),
30                 ("  ImpQual        ", import_qual),
31                 ("  ImpAs          ", import_as),
32                 ("  ImpAll         ", import_all),
33                 ("  ImpPartial     ", import_partial),
34                 ("  ImpHiding      ", import_hiding),
35                 ("FixityDecls      ", fixity_ds),
36                 ("DefaultDecls     ", default_ds),
37                 ("TypeDecls        ", type_ds),
38                 ("DataDecls        ", data_ds),
39                 ("NewTypeDecls     ", newt_ds),
40                 ("DataConstrs      ", data_constrs),
41                 ("DataDerivings    ", data_derivs),
42                 ("ClassDecls       ", class_ds),
43                 ("ClassMethods     ", class_method_ds),
44                 ("DefaultMethods   ", default_method_ds),
45                 ("InstDecls        ", inst_ds),
46                 ("InstMethods      ", inst_method_ds),
47                 ("TypeSigs         ", bind_tys),
48                 ("ValBinds         ", val_bind_ds),
49                 ("FunBinds         ", fn_bind_ds),
50                 ("InlineMeths      ", method_inlines),
51                 ("InlineBinds      ", bind_inlines),
52 --              ("SpecialisedData  ", data_specs),
53 --              ("SpecialisedInsts ", inst_specs),
54                 ("SpecialisedMeths ", method_specs),
55                 ("SpecialisedBinds ", bind_specs)
56                ])
57   where
58     pp_val (str, 0) = empty
59     pp_val (str, n) 
60       | not short   = hcat [text str, int n]
61       | otherwise   = hcat [text (trim str), equals, int n, semi]
62     
63     trim ls     = takeWhile (not.isSpace) (dropWhile isSpace ls)
64
65     fixity_ds   = length [() | FixD d <- decls]
66                 -- NB: this omits fixity decls on local bindings and
67                 -- in class decls.  ToDo
68
69     tycl_decls  = [d | TyClD d <- decls]
70     (class_ds, data_ds, newt_ds, type_ds, _) = countTyClDecls tycl_decls
71
72     inst_decls  = [d | InstD d <- decls]
73     inst_ds     = length inst_decls
74     default_ds  = length [() | DefD _ <- decls]
75     val_decls   = [d | ValD d <- decls]
76
77     real_exports = case exports of { Nothing -> []; Just es -> es }
78     n_exports    = length real_exports
79     export_ms    = length [() | IEModuleContents _ <- real_exports]
80     export_ds    = n_exports - export_ms
81     export_all   = case exports of { Nothing -> 1; other -> 0 }
82
83     (val_bind_ds, fn_bind_ds, bind_tys, bind_specs, bind_inlines)
84         = count_binds (foldr ThenBinds EmptyBinds val_decls)
85
86     (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
87         = foldr add6 (0,0,0,0,0,0) (map import_info imports)
88     (data_constrs, data_derivs)
89         = foldr add2 (0,0) (map data_info tycl_decls)
90     (class_method_ds, default_method_ds)
91         = foldr add2 (0,0) (map class_info tycl_decls)
92     (inst_method_ds, method_specs, method_inlines)
93         = foldr add3 (0,0,0) (map inst_info inst_decls)
94
95
96     count_binds EmptyBinds        = (0,0,0,0,0)
97     count_binds (ThenBinds b1 b2) = count_binds b1 `add5` count_binds b2
98     count_binds (MonoBind b sigs _) = case (count_monobinds b, count_sigs sigs) of
99                                         ((vs,fs),(ts,_,ss,is)) -> (vs,fs,ts,ss,is)
100
101     count_monobinds EmptyMonoBinds                 = (0,0)
102     count_monobinds (AndMonoBinds b1 b2)           = count_monobinds b1 `add2` count_monobinds b2
103     count_monobinds (PatMonoBind (VarPatIn n) r _) = (1,0)
104     count_monobinds (PatMonoBind p r _)            = (0,1)
105     count_monobinds (FunMonoBind f _ m _)          = (0,1)
106
107     count_mb_monobinds (Just mbs) = count_monobinds mbs
108     count_mb_monobinds Nothing    = (0,0)
109
110     count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
111
112     sig_info (Sig _ _ _)            = (1,0,0,0)
113     sig_info (ClassOpSig _ _ _ _)   = (0,1,0,0)
114     sig_info (SpecSig _ _ _)        = (0,0,1,0)
115     sig_info (InlineSig _ _ _)      = (0,0,0,1)
116     sig_info (NoInlineSig _ _ _)    = (0,0,0,1)
117     sig_info _                      = (0,0,0,0)
118
119     import_info (ImportDecl _ _ qual as spec _)
120         = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
121     qual_info False  = 0
122     qual_info True   = 1
123     as_info Nothing  = 0
124     as_info (Just _) = 1
125     spec_info Nothing           = (0,0,0,1,0,0)
126     spec_info (Just (False, _)) = (0,0,0,0,1,0)
127     spec_info (Just (True, _))  = (0,0,0,0,0,1)
128
129     data_info (TyData {tcdNCons = nconstrs, tcdDerivs = derivs})
130         = (nconstrs, case derivs of {Nothing -> 0; Just ds -> length ds})
131     data_info other = (0,0)
132
133     class_info decl@(ClassDecl {})
134         = case count_sigs (tcdSigs decl) of
135             (_,classops,_,_) ->
136                (classops, addpr (count_mb_monobinds (tcdMeths decl)))
137     class_info other = (0,0)
138
139     inst_info (InstDecl _ inst_meths inst_sigs _ _)
140         = case count_sigs inst_sigs of
141             (_,_,ss,is) ->
142                (addpr (count_monobinds inst_meths), ss, is)
143
144     addpr :: (Int,Int) -> Int
145     add1  :: Int -> Int -> Int
146     add2  :: (Int,Int) -> (Int,Int) -> (Int, Int)
147     add3  :: (Int,Int,Int) -> (Int,Int,Int) -> (Int, Int, Int)
148     add4  :: (Int,Int,Int,Int) -> (Int,Int,Int,Int) -> (Int, Int, Int, Int)
149     add5  :: (Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int)
150     add6  :: (Int,Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int, Int)
151
152     addpr (x,y) = x+y
153     add1 x1 y1  = x1+y1
154     add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
155     add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
156     add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
157     add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
158     add6 (x1,x2,x3,x4,x5,x6) (y1,y2,y3,y4,y5,y6) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5,x6+y6)
159 \end{code}
160
161
162
163
164
165
166
167
168