[project @ 2003-10-09 11:58:39 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 import Util             ( count )
15 \end{code}
16
17 %************************************************************************
18 %*                                                                      *
19 \subsection{Statistics}
20 %*                                                                      *
21 %************************************************************************
22
23 \begin{code}
24 ppSourceStats short (HsModule _ exports imports decls _ src_loc)
25  = (if short then hcat else vcat)
26         (map pp_val
27                [("ExportAll        ", export_all), -- 1 if no export list
28                 ("ExportDecls      ", export_ds),
29                 ("ExportModules    ", export_ms),
30                 ("Imports          ", import_no),
31                 ("  ImpQual        ", import_qual),
32                 ("  ImpAs          ", import_as),
33                 ("  ImpAll         ", import_all),
34                 ("  ImpPartial     ", import_partial),
35                 ("  ImpHiding      ", import_hiding),
36                 ("FixityDecls      ", fixity_sigs),
37                 ("DefaultDecls     ", default_ds),
38                 ("TypeDecls        ", type_ds),
39                 ("DataDecls        ", data_ds),
40                 ("NewTypeDecls     ", newt_ds),
41                 ("DataConstrs      ", data_constrs),
42                 ("DataDerivings    ", data_derivs),
43                 ("ClassDecls       ", class_ds),
44                 ("ClassMethods     ", class_method_ds),
45                 ("DefaultMethods   ", default_method_ds),
46                 ("InstDecls        ", inst_ds),
47                 ("InstMethods      ", inst_method_ds),
48                 ("TypeSigs         ", bind_tys),
49                 ("ValBinds         ", val_bind_ds),
50                 ("FunBinds         ", fn_bind_ds),
51                 ("InlineMeths      ", method_inlines),
52                 ("InlineBinds      ", bind_inlines),
53 --              ("SpecialisedData  ", data_specs),
54 --              ("SpecialisedInsts ", inst_specs),
55                 ("SpecialisedMeths ", method_specs),
56                 ("SpecialisedBinds ", bind_specs)
57                ])
58   where
59     pp_val (str, 0) = empty
60     pp_val (str, n) 
61       | not short   = hcat [text str, int n]
62       | otherwise   = hcat [text (trim str), equals, int n, semi]
63     
64     trim ls     = takeWhile (not.isSpace) (dropWhile isSpace ls)
65
66     (fixity_sigs, bind_tys, bind_specs, bind_inlines) 
67         = count_sigs [d | SigD d <- decls]
68                 -- NB: this omits fixity decls on local bindings and
69                 -- in class decls.  ToDo
70
71     tycl_decls  = [d | TyClD d <- decls]
72     (class_ds, type_ds, data_ds, newt_ds) = countTyClDecls tycl_decls
73
74     inst_decls  = [d | InstD d <- decls]
75     inst_ds     = length inst_decls
76     default_ds  = count (\ x -> case x of { DefD{} -> True; _ -> False}) decls
77     val_decls   = [d | ValD d <- decls]
78
79     real_exports = case exports of { Nothing -> []; Just es -> es }
80     n_exports    = length real_exports
81     export_ms    = count (\ e -> case e of { IEModuleContents{} -> True;_ -> False})
82                          real_exports
83     export_ds    = n_exports - export_ms
84     export_all   = case exports of { Nothing -> 1; other -> 0 }
85
86     (val_bind_ds, fn_bind_ds)
87         = foldr add2 (0,0) (map count_monobinds val_decls)
88
89     (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
90         = foldr add6 (0,0,0,0,0,0) (map import_info imports)
91     (data_constrs, data_derivs)
92         = foldr add2 (0,0) (map data_info tycl_decls)
93     (class_method_ds, default_method_ds)
94         = foldr add2 (0,0) (map class_info tycl_decls)
95     (inst_method_ds, method_specs, method_inlines)
96         = foldr add3 (0,0,0) (map inst_info inst_decls)
97
98     count_monobinds EmptyMonoBinds               = (0,0)
99     count_monobinds (AndMonoBinds b1 b2)         = count_monobinds b1 `add2` count_monobinds b2
100     count_monobinds (PatMonoBind (VarPat n) r _) = (1,0)
101     count_monobinds (PatMonoBind p r _)          = (0,1)
102     count_monobinds (FunMonoBind f _ m _)        = (0,1)
103
104     count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
105
106     sig_info (FixSig _)             = (1,0,0,0)
107     sig_info (Sig _ _ _)            = (0,1,0,0)
108     sig_info (SpecSig _ _ _)        = (0,0,1,0)
109     sig_info (InlineSig _ _ _ _)    = (0,0,0,1)
110     sig_info _                      = (0,0,0,0)
111
112     import_info (ImportDecl _ _ qual as spec _)
113         = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
114     qual_info False  = 0
115     qual_info True   = 1
116     as_info Nothing  = 0
117     as_info (Just _) = 1
118     spec_info Nothing           = (0,0,0,1,0,0)
119     spec_info (Just (False, _)) = (0,0,0,0,1,0)
120     spec_info (Just (True, _))  = (0,0,0,0,0,1)
121
122     data_info (TyData {tcdCons = cs, tcdDerivs = derivs})
123         = (length cs, case derivs of {Nothing -> 0; Just ds -> length ds})
124     data_info other = (0,0)
125
126     class_info decl@(ClassDecl {})
127         = case count_sigs (tcdSigs decl) of
128             (_,classops,_,_) ->
129                (classops, addpr (count_monobinds (tcdMeths decl)))
130     class_info other = (0,0)
131
132     inst_info (InstDecl _ inst_meths inst_sigs _)
133         = case count_sigs inst_sigs of
134             (_,_,ss,is) ->
135                (addpr (count_monobinds inst_meths), ss, is)
136
137     addpr :: (Int,Int) -> Int
138     add2  :: (Int,Int) -> (Int,Int) -> (Int, Int)
139     add3  :: (Int,Int,Int) -> (Int,Int,Int) -> (Int, Int, Int)
140     add4  :: (Int,Int,Int,Int) -> (Int,Int,Int,Int) -> (Int, Int, Int, Int)
141     add6  :: (Int,Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int, Int)
142
143     addpr (x,y) = x+y
144     add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
145     add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
146     add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
147     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)
148 \end{code}
149
150
151
152
153
154
155
156
157