Amend comment per Marlow's comments.
[ghc-hetmet.git] / 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 SrcLoc
14 import Bag
15 import Util
16 import RdrName
17
18 import Data.Char
19 \end{code}
20
21 %************************************************************************
22 %*                                                                      *
23 \subsection{Statistics}
24 %*                                                                      *
25 %************************************************************************
26
27 \begin{code}
28 ppSourceStats :: Bool -> Located (HsModule RdrName) -> SDoc
29 ppSourceStats short (L _ (HsModule _ exports imports ldecls _ _))
30  = (if short then hcat else vcat)
31         (map pp_val
32                [("ExportAll        ", export_all), -- 1 if no export list
33                 ("ExportDecls      ", export_ds),
34                 ("ExportModules    ", export_ms),
35                 ("Imports          ", import_no),
36                 ("  ImpQual        ", import_qual),
37                 ("  ImpAs          ", import_as),
38                 ("  ImpAll         ", import_all),
39                 ("  ImpPartial     ", import_partial),
40                 ("  ImpHiding      ", import_hiding),
41                 ("FixityDecls      ", fixity_sigs),
42                 ("DefaultDecls     ", default_ds),
43                 ("TypeDecls        ", type_ds),
44                 ("DataDecls        ", data_ds),
45                 ("NewTypeDecls     ", newt_ds),
46                 ("TypeFamilyDecls  ", type_fam_ds),
47                 ("FamilyInstDecls  ", fam_inst_ds),
48                 ("DataConstrs      ", data_constrs),
49                 ("DataDerivings    ", data_derivs),
50                 ("ClassDecls       ", class_ds),
51                 ("ClassMethods     ", class_method_ds),
52                 ("DefaultMethods   ", default_method_ds),
53                 ("InstDecls        ", inst_ds),
54                 ("InstMethods      ", inst_method_ds),
55                 ("InstType         ", inst_type_ds),
56                 ("InstData         ", inst_data_ds),
57                 ("TypeSigs         ", bind_tys),
58                 ("ValBinds         ", val_bind_ds),
59                 ("FunBinds         ", fn_bind_ds),
60                 ("InlineMeths      ", method_inlines),
61                 ("InlineBinds      ", bind_inlines),
62 --              ("SpecialisedData  ", data_specs),
63 --              ("SpecialisedInsts ", inst_specs),
64                 ("SpecialisedMeths ", method_specs),
65                 ("SpecialisedBinds ", bind_specs)
66                ])
67   where
68     decls = map unLoc ldecls
69
70     pp_val (_, 0) = empty
71     pp_val (str, n) 
72       | not short   = hcat [text str, int n]
73       | otherwise   = hcat [text (trim str), equals, int n, semi]
74     
75     trim ls     = takeWhile (not.isSpace) (dropWhile isSpace ls)
76
77     (fixity_sigs, bind_tys, bind_specs, bind_inlines) 
78         = count_sigs [d | SigD d <- decls]
79                 -- NB: this omits fixity decls on local bindings and
80                 -- in class decls.  ToDo
81
82     tycl_decls  = [d | TyClD d <- decls]
83     (class_ds, type_ds, data_ds, newt_ds, type_fam_ds, fam_inst_ds) = 
84       countTyClDecls tycl_decls
85
86     inst_decls  = [d | InstD d <- decls]
87     inst_ds     = length inst_decls
88     default_ds  = count (\ x -> case x of { DefD{} -> True; _ -> False}) decls
89     val_decls   = [d | ValD d <- decls]
90
91     real_exports = case exports of { Nothing -> []; Just es -> es }
92     n_exports    = length real_exports
93     export_ms    = count (\ e -> case unLoc e of { IEModuleContents{} -> True;_ -> False})
94                          real_exports
95     export_ds    = n_exports - export_ms
96     export_all   = case exports of { Nothing -> 1; _ -> 0 }
97
98     (val_bind_ds, fn_bind_ds)
99         = foldr add2 (0,0) (map count_bind val_decls)
100
101     (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
102         = foldr add6 (0,0,0,0,0,0) (map import_info imports)
103     (data_constrs, data_derivs)
104         = foldr add2 (0,0) (map data_info tycl_decls)
105     (class_method_ds, default_method_ds)
106         = foldr add2 (0,0) (map class_info tycl_decls)
107     (inst_method_ds, method_specs, method_inlines, inst_type_ds, inst_data_ds)
108         = foldr add5 (0,0,0,0,0) (map inst_info inst_decls)
109
110     count_bind (PatBind { pat_lhs = L _ (VarPat _) }) = (1,0)
111     count_bind (PatBind {})                           = (0,1)
112     count_bind (FunBind {})                           = (0,1)
113     count_bind b = pprPanic "count_bind: Unhandled binder" (ppr b)
114
115     count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
116
117     sig_info (FixSig _)         = (1,0,0,0)
118     sig_info (TypeSig _ _)      = (0,1,0,0)
119     sig_info (SpecSig _ _ _)    = (0,0,1,0)
120     sig_info (InlineSig _ _)    = (0,0,0,1)
121     sig_info _                  = (0,0,0,0)
122
123     import_info (L _ (ImportDecl _ _ _ qual as spec))
124         = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
125     qual_info False  = 0
126     qual_info True   = 1
127     as_info Nothing  = 0
128     as_info (Just _) = 1
129     spec_info Nothing           = (0,0,0,1,0,0)
130     spec_info (Just (False, _)) = (0,0,0,0,1,0)
131     spec_info (Just (True, _))  = (0,0,0,0,0,1)
132
133     data_info (TyData {tcdCons = cs, tcdDerivs = derivs})
134         = (length cs, case derivs of Nothing -> 0
135                                      Just ds -> length ds)
136     data_info _ = (0,0)
137
138     class_info decl@(ClassDecl {})
139         = case count_sigs (map unLoc (tcdSigs decl)) of
140             (_,classops,_,_) ->
141                (classops, addpr (foldr add2 (0,0) (map (count_bind.unLoc) (bagToList (tcdMeths decl)))))
142     class_info _ = (0,0)
143
144     inst_info (InstDecl _ inst_meths inst_sigs ats)
145         = case count_sigs (map unLoc inst_sigs) of
146             (_,_,ss,is) ->
147               case foldr add2 (0, 0) (map (countATDecl . unLoc) ats) of
148                 (tyDecl, dtDecl) ->
149                   (addpr (foldr add2 (0,0) 
150                            (map (count_bind.unLoc) (bagToList inst_meths))), 
151                    ss, is, tyDecl, dtDecl)
152         where
153           countATDecl (TyData    {}) = (0, 1)
154           countATDecl (TySynonym {}) = (1, 0)
155           countATDecl d              = pprPanic "countATDecl: Unhandled decl"
156                                             (ppr d)
157
158     addpr :: (Int,Int) -> Int
159     add2  :: (Int,Int) -> (Int,Int) -> (Int, Int)
160     add4  :: (Int,Int,Int,Int) -> (Int,Int,Int,Int) -> (Int, Int, Int, Int)
161     add5  :: (Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int)
162     add6  :: (Int,Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int, Int)
163
164     addpr (x,y) = x+y
165     add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
166     add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
167     add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
168     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)
169 \end{code}
170
171
172
173
174
175
176
177
178